Ren*_*osa 15 c# number-formatting visual-studio winforms
我需要写出如下值:
9.6 x 10²
9.6 x 10¹²
Run Code Online (Sandbox Code Playgroud)
我需要知道是否有一种方法可以在字符串中格式化数字.
Pat*_*man 12
您必须从您正在使用的代码页中找到适当的字符,例如UTF-8:
string superScript2 = "²";
Run Code Online (Sandbox Code Playgroud)
在字符串中没有格式化,它只是所有数据.
ja7*_*a72 11
试试这个:
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)}";
}
}
Run Code Online (Sandbox Code Playgroud)
作为测试案例运行
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;
}
}
Run Code Online (Sandbox Code Playgroud)
与输出:
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²?
Run Code Online (Sandbox Code Playgroud)
绝不优化,但它确实起作用.指数是工程形式(仅为3的倍数,以避免类似的事情10¹).作为奖励,通过分别提供类似g4或g54或5位数的格式代码,可以将数字格式化为特定数量的有效数字.
NAN或Inf.作为我上面评论的后续 - 这样的事情是否可以满足您的要求:
public 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);
}
Run Code Online (Sandbox Code Playgroud)
请参阅: https: //dotnetfiddle.net/dX7LAF
根据我上面的评论 - 该数字首先转换为指数格式字符串(https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#EFormatString),然后该字符串在指数分隔符“E”上拆分。第一个数组是数字部分,第二个数组是 10 的幂 - 使用我给出的链接技术之一将其转换为上标字符(将字符串/整数转换为 C# 中的上标),然后转换回来到一个字符串&使用“x 10”作为新分隔符组合两个部分。
我假设您希望按照您的示例将值设置为单位数精度,并且前面没有 + 号。如果您需要其他任何内容,可以将格式作为参数传递。上标+的代码是'\u207A'。这里有一个链接(在撰写本文时)给出了上标代码列表:http://unicode.org/charts/PDF/U2070.pdf
| 归档时间: |
|
| 查看次数: |
1985 次 |
| 最近记录: |