简体   繁体   English

浮动到字符串转换

[英]Float to String Conversion

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.50 because 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? 如果有办法,请建议我吗?

You should be using {0:N2} to format to two decimal places. 您应该使用{0:N2}格式化为两位小数。

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);

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.50 and 2.500 are exactly the same. 的浮点表示, 2.52.502.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 ). 将两个值都转换为float,然后比较它们(由于浮点数不准确 ,因此误差范围)。
  • Store the number of decimal places of the old value and then use myFloat.ToString("F" + numDecimals.ToString()) to convert it to a string. 存储旧值的小数位数,然后使用myFloat.ToString("F" + numDecimals.ToString())将其转换为字符串。
  • Store the value as a string instead of a float. 将值存储为字符串而不是浮点数。 Obviously, you won't be able to do math on that value. 显然,你将无法对该值进行数学计算。

Alternatively, if you do not insist on using floats, decimals might suit your purpose: The do store the number of significant digits: 另外,如果你不使用浮动坚持, decimals可能适合你的目的:在存储显著的位数:

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

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. 版本1和2几乎相似,但3是不同的。 3 will include number separators when number is large. 当数量很大时,3将包括数字分隔符。

For example the following outputs 454,542.50 例如,以下输出454,542.50

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

More on MSDN 更多关于MSDN

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

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