简体   繁体   English

如何在C#中将2.0转换为四个精度浮点数

[英]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.

相关问题 如何从 .NET 的 BinaryWriter 序列化的四个字节中读取单精度浮点数 - How to read a single-precision floating-point number from four bytes serialized by .NET's BinaryWriter 输出浮点宽度和精度C#? - Output floating point width and precision C#? 我需要在C#中将大端单精度浮点数写入文件 - I need to write a big-endian single-precision floating point number to file in C# C#浮点数的显示和计算精度不同 - C# floating point number's precision of display and calculation, which are different C#-如何将浮点数/指数结果转换/显示为完整/整数 - C# - How To Convert/Display a Floating-Point/Exponential Result into a Full/Whole Number C 和 C# 之间浮点精度行为的差异 - Difference in floating point precision behavior between C and C# 如何在C#中将浮点字符串转换为int - How do i convert a floating point string to an int in c# 将 c# float 转换为 IEEE 单精度浮点字节 - converting c# float to IEEE single precision floating point bytes C#浮点数前一位 - C# one number before floating point 如何以与 JavaScript's.toString(36) 相同的方式将 C# 中的浮点数转换为 Base36 - How to convert a floating point number to Base36 in C# in the same way as JavaScript's .toString(36) does it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM