我需要写入像这样的数值:
9.6 x 10²
9.6 x 10¹²
我需要知道是否有一种方法可以在字符串中格式化数字,就像上面显示的那样。
我需要写入像这样的数值:
9.6 x 10²
9.6 x 10¹²
我需要知道是否有一种方法可以在字符串中格式化数字,就像上面显示的那样。
试试这个:
public static string Eng(this double x, string format="g")
{
const string sup_signs = "⁺⁻⁼⁽⁾ⁿ";
const string sup_digits = "⁰¹²³⁴⁵⁶⁷⁸⁹";
if(double.IsNaN(x) || double.IsInfinity(x))
{
return x.ToString();
}
int num_sign = Math.Sign(x);
x = Math.Abs(x);
// group exponents in multiples of 3 (thousands)
int exp = (int)Math.Floor(Math.Log(x, 10)/3)*3;
// otherwise use:
// int exp = (int)Math.Floor(Math.Log(x, 10));
// and handle the exp==1 case separetly to avoid 10¹
x*= Math.Pow(10, -exp);
int exp_sign = Math.Sign(exp);
exp = Math.Abs(exp);
// Build the exponent string 'dig' from right to left
string dig = string.Empty;
while(exp>0)
{
int n = exp%10;
dig = sup_digits[n] + dig;
exp = exp/10;
}
// if has exponent and its negative prepend the superscript minus sign
if(dig.Length>0 && exp_sign<0)
{
dig = sup_signs[1] + dig;
}
// prepend answer with minus if number is negative
string sig = num_sign<0 ? "-" : "";
if(dig.Length>0)
{
// has exponent
return $"{sig}{x.ToString(format)}×10{dig}";
}
else
{
// no exponent
return $"{sig}{x.ToString(format)}";
}
}
作为一个测试案例运行
static void Main(string[] args)
{
// Type code here.
double x = Math.PI/50e5;
for(int i = 0; i < 20; i++)
{
// Format output to 12 wide column, right aligned
Debug.WriteLine($"{ Eng(x, "g4"),12}");
x*=50;
}
}
输出结果为:
628.3×10⁻⁹
31.42×10⁻⁶
1.571×10⁻³
78.54×10⁻³
3.927
196.3
9.817×10³
490.9×10³
24.54×10⁶
1.227×10⁹
61.36×10⁹
3.068×10¹²
153.4×10¹²
7.67×10¹⁵
383.5×10¹⁵
19.17×10¹⁸
958.7×10¹⁸
47.94×10²¹
2.397×10²⁴
119.8×10²⁴
这个算法并不是最优化的,但它确实完成了工作。幂指数采用工程表示法(仅为3的倍数,以避免出现像10¹
这样的东西)。作为奖励,可以通过提供格式代码(如g4
或g5
)来将数字格式化为指定数量的有效数字,分别为4或5位。
NAN
或Inf
1.23e2
,我更喜欢 123
。 - John Alexioupublic String FormatAs10Power(decimal val)
{
string SuperscriptDigits = "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
string expstr = String.Format("{0:0.#E0}", val);
var numparts = expstr.Split('E');
char[] powerchars = numparts[1].ToArray();
for (int i = 0; i < powerchars.Length; i++)
{
powerchars[i] = (powerchars[i] == '-') ? '\u207b' : SuperscriptDigits[powerchars[i] - '0'];
}
numparts[1] = new String(powerchars);
return String.Join(" x 10",numparts);
}
请参见:https://dotnetfiddle.net/dX7LAF
根据我上面的评论 - 数字首先被转换为指数格式字符串(https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#EFormatString),然后在指数分隔符'E'上拆分该字符串。第一个数组是数字部分,第二个是它所提高的10的幂次 - 这使用我给出的链接中的技术之一转换为上标字符(Convert a string/integer to superscript in C#),再将其转换回字符串,并使用“x 10”作为新的分隔符组合两个部分。
我已经假设你希望按照你的示例将值精确到个位数,且没有前导的+号。如果需要其他内容,您可以将格式作为参数传递。上标 + 的代码为 '\u207A'。这里有一个链接(在撰写本文时)提供了上标代码列表:http://unicode.org/charts/PDF/U2070.pdf
String.Format()
方法中有一个指数数字格式说明符,它可以给出带有指数的数字(如1.2346e+004
),虽然不完全与您的示例格式相同,但具有相同的含义。更多信息:https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#EFormatString - Diado