简体   繁体   English

C#圆形科学记数值

[英]C# Round Scientific Notation Value

I have a small double value that is formatted in scientific notation. 我有一个小的双值,用科学记数法格式化。 When I display the value it is displayed in the format 1.34423E-12 with 5 digits after the decimal. 当我显示该值时,它以1.34423E-12格式显示,小数点后5位数。 I would like to round this value so that it displays like 1.344E-12. 我想舍入这个值,使其显示为1.344E-12。

Is there a built in way to round a scientific notation value to x number of decimals? 是否有内置的方法将科学记数法值舍入为x个小数位?

Use a format string like 使用格式字符串

double d = 1.34423e-12;
string formattedValue = d.ToString("E3");

Here "E" means to use scientific notation (with capital "E", use "e" if you want a small one...) and 3 stands for three digits after the decimal point. 这里"E"表示使用科学记数法(使用大写“E”,如果你想要一个小的,则使用"e" ......)和3代表小数点后的三位数。

You can look at Standard numeric format strings at MSDN to see other options. 您可以在MSDN上查看标准数字格式字符串以查看其他选项。 The documentation for the String.Format method also contains useful information about formatting values. String.Format方法的文档还包含有关格式化值的有用信息。

EDIT 编辑

If you want more flexibility, you can use Custom numeric Format Strings . 如果您想要更多灵活性,可以使用自定义数字格式字符串 Using these you can eg also specify the number of digits used for the exponent like 使用这些,你可以例如指定用于指数的数字位数

d.ToString("0.000E0"); // -> Results in "13.344E-12" instead of "13.344E-012"
d.ToString("0.000E0000"); // -> "13.344E-0012"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM