[英]how to convert 2.0 to four precision floating point number in c#
I have the following code: 我有以下代码:
float f = 0.02;
The results are equivalent to: 结果等同于:
f = 0.0200
How to do this in C#? 如何在C#中做到这一点?
A floating point number has a value (for example 0.02). 浮点数具有一个值 (例如0.02)。
When you print it, you can format it into any number of different representations, including "0.002", "0.00200" and "scientific notation". 打印时,可以将其格式化为任意数量的不同表示形式,包括“ 0.002”,“ 0.00200”和“科学符号”。 It's the same value with the same precision - it's just printed differently (perhaps with a different number of digits after the decimal).
它是具有相同精度的相同值-只是以不同的方式打印(可能在小数点后使用不同的数字位数)。
I believe your question is about "formatting": 我相信您的问题与“格式”有关:
String s = String.Format("{0:0.0000}", 0.002);
Here is much more detail: 这里有更多细节:
Under the hood, the number is still the same whether the trailing zeros are shown. 在引擎盖下,无论是否显示尾随零,该数字均相同。 You can't change what the number is , but you can change how it displays by using a .ToString() overide.
您不能更改数字是什么,但是可以使用.ToString()覆盖来更改数字的显示方式。
Specifically the Zero custom specifier . 特别是零自定义说明符 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.