Mar*_*arc 5 c# double scientific-notation string-formatting
我正在尝试以科学格式格式化一些大数字,但我需要三倍的倍数.有推荐的方法吗?
我在表格中有一系列数字,而不是真正的科学格式(小数点前面有一个数字)我很高兴有这个改变,以获得三倍的幂,例如:
3.123e + 003
19.523e + 003
而不是:
3.123e + 003
1.952e + 004
我相信拥有所有权力是三倍的倍数使我的桌子更容易扫描.
提前致谢
我认为你需要编写自己的函数。首先,您可以获得数字的科学表示,其精度比您需要的大两位数。然后您应该解析结果字符串以获取浮点系数和 10 的幂索引作为十进制类型的数字。之后,分析索引除以 3 的余数,并以适当的方式更改数字。最后生成输出字符串。
static string Scientific3(double value, int precision)
{
string fstr1 = "{0:E" + (precision+2).ToString() + "}";
string step1 = string.Format(fstr1, value);
int index1 = step1.ToLower().IndexOf('e');
if (index1 < 0 || index1 == step1.Length - 1)
throw new Exception();
decimal coeff = Convert.ToDecimal(step1.Substring(0, index1));
int index = Convert.ToInt32(step1.Substring(index1 + 1));
while(index%3!=0)
{
index--;
coeff *= 10;
}
string fstr2 = "{0:F" + precision.ToString() + "}E{1}{2:D3}";
return string.Format(fstr2, coeff, ((index < 0) ? "-" : "+"), Math.Abs(index));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1833 次 |
| 最近记录: |