C# 浮点数到字符串的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19043899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:58:40  来源:igfitidea点击:

Float to String Conversion

c#stringtype-conversion

提问by Nomesh Gajare

I want to convert float value to string.

我想将浮点值转换为字符串。

Below is the code which i am using for the conversion.

下面是我用于转换的代码。

static void Main(string[] args)
        {
            string s =string.Format("{0:G}", value);                
            Console.Write(s);
            Console.ReadLine();
        }

and it outputs as 2.5

它输出为 2.5

But my problem is i want to get the value as 2.50because i want to compare it with original value later in my project.

但我的问题是我想获得价值,2.50因为我想在我的项目后期将它与原始价值进行比较。

so please suggest me if there are any ways to do it?

所以请建议我是否有任何方法可以做到这一点?

采纳答案by Sadique

You should be using {0:N2}to format to two decimal places.

您应该使用{0:N2}to 格式化为两位小数。

string.Format("{0:N2}", 2.50)

For 3 decimal places:

对于 3 个小数位:

string.Format("{0:N3}", 2.50)

And so on.

等等。

You can also store the value in a string this way without worrying about precision and then convert your value where you are testing for comparison as string:

您还可以通过这种方式将值存储在字符串中,而无需担心精度,然后将您要测试的值转换为字符串:

string strDecimalVal = Convert.ToString( 2.5000001);

回答by Sriram Sakthivel

Try this

尝试这个

Console.WriteLine("{0:F2}", 2.50);
Console.WriteLine("{0:0.00}", 2.50);
Console.WriteLine("{0:N2}", 2.50);

Version 1 and 2 are almost similar, but 3 is different. 3 will include number separators when number is large.

版本 1 和 2 几乎相似,但版本 3 不同。当数字很大时,3 将包括数字分隔符。

For example the following outputs 454,542.50

例如以下输出 454,542.50

Console.WriteLine("{0:N2}", 454542.50);

More on MSDN

更多关于MSDN

回答by Heinzi

because i want to compare it with original value later in my project.

因为我想在我的项目后期将它与原始值进行比较。

...then you will need to store the number of decimal places the original value had. Once the value is a float, this information is lost. The float representations of 2.5, 2.50and 2.500are exactly the same.

...然后您将需要存储原始值的小数位数。一旦值为浮点数,此信息就会丢失。的浮动交涉2.52.50并且2.500是完全一样的。

So, basically, you have the following possibilities (in order of preference):

因此,基本上,您有以下可能性(按优先顺序):

  • Don't do a string comparison between the old and the new value. Convert both values to float and then compare them (with a margin of error since floats are not precise).
  • Store the number of decimal places of the old value and then use myFloat.ToString("F" + numDecimals.ToString())to convert it to a string.
  • Store the value as a string instead of a float. Obviously, you won't be able to do math on that value.
  • 不要在旧值和新值之间进行字符串比较。将两个值都转换为浮点数,然后比较它们(由于浮点数不精确,因此存在一定的误差)。
  • 存储旧值的小数位数,然后用于myFloat.ToString("F" + numDecimals.ToString())将其转换为字符串。
  • 将值存储为字符串而不是浮点数。显然,您将无法对该值进行数学运算。

Alternatively, if you do not insist on using floats, decimalsmight suit your purpose: The dostore the number of significant digits:

或者,如果您不坚持使用浮点数,则decimals可能适合您的目的:do存储有效位数:

decimal x = Decimal.Parse("2.50", CultureInfo.InvariantCulture);
decimal y = Decimal.Parse("2.500", CultureInfo.InvariantCulture);

Console.WriteLine(x.ToString()); // prints 2.50
Console.WriteLine(y.ToString()); // prints 2.500