将浮点数格式化为2位小数

Cal*_*lum 198 c# variables floating-point decimal

我目前正在为客户网站建立销售模块.到目前为止,我已经得到了完美计算的销售价格,但我遇到的问题是将输出格式化为2位小数.

我目前在一个变量中调用它,以便我可以将数据绑定到listview.

Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())),
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何将输出格式化为2位小数?非常感谢!

Wra*_*ath 451

您可以将格式传递给ToString方法,例如:

myFloatVariable.ToString("0.00"); //2dp Number

myFloatVariable.ToString("n2"); // 2dp Number

myFloatVariable.ToString("c2"); // 2dp currency
Run Code Online (Sandbox Code Playgroud)

标准数字格式字符串

  • "N2"和"C2"将添加千位分隔符,而"0.00"则不会. (35认同)
  • 我应该能够+2这篇文章的货币提示. (10认同)
  • 如果您不确定(我是),所有这些格式化浮点值的方法都会提供舍入. (4认同)

Mic*_*rdt 44

您需要做的第一件事是使用decimal类型而不是float价格.使用float绝对是不可接受的,因为它无法准确地表示大多数小数部分.

完成后,Decimal.Round()可以用于舍入到2个位置.

  • 在这里称 float“绝对不可接受”有点夸张,但使用 Decimal 类的想法肯定是好的。 (3认同)

ale*_*rul 36

String.Format("{0:#,###.##}", value)

C#中String Stringting的一个更复杂的例子:

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);

如果超过1243.50,这将输出"$ 1,240.00".它将输出相同的格式,但如果数字为负,则输出括号,如果数字为零,则输出字符串"Zero".


Bit*_*Bit 22

我相信:

的String.Format( "{0:0.00}",售);

应该这样做.

请参阅链接 字符串格式示例C#


saa*_*stn 16

这适用于您想要使用内插字符串的情况。我实际上发布这个是因为我厌倦了反复试验,最终每次我需要格式化一些标量时都要滚动浏览大量文档。

$"{1234.5678:0.00}"        "1234.57"        2 decimal places, notice that value is rounded
$"{1234.5678,10:0.00}"     "   1234.57"     right-aligned
$"{1234.5678,-10:0.00}"    "1234.57   "     left-aligned
$"{1234.5678:0.#####}"     "1234.5678"      5 optional digits after the decimal point
$"{1234.5678:0.00000}"     "1234.56780"     5 forced digits AFTER the decimal point, notice the trailing zero
$"{1234.5678:00000.00}"    "01234.57"       5 forced digits BEFORE the decimal point, notice the leading zero
$"{1234.5612:0}"           "1235"           as integer, notice that value is rounded
$"{1234.5678:F2}"          "1234.57"        standard fixed-point
$"{1234.5678:F5}"          "1234.56780"     5 digits after the decimal point, notice the trailing zero
$"{1234.5678:g2}"          "1.2e+03"        standard general with 2 meaningful digits, notice "e"
$"{1234.5678:G2}"          "1.2E+03"        standard general with 2 meaningful digits, notice "E"
$"{1234.5678:G3}"          "1.23E+03"       standard general with 3 meaningful digits
$"{1234.5678:G5}"          "1234.6"         standard general with 5 meaningful digits
$"{1234.5678:e2}"          "1.23e+003"      standard exponential with 2 digits after the decimal point, notice "e"
$"{1234.5678:E3}"          "1.235E+003"     standard exponential with 3 digits after the decimal point, notice "E"
$"{1234.5678:N2}"          "1,234.57"       standard numeric, notice the comma
$"{1234.5678:C2}"          "$1,234.57"      standard currency, notice the dollar sign
$"{1234.5678:P2}"          "123,456.78 %"   standard percent, notice that value is multiplied by 100
$"{1234.5678:2}"           "2"              :)
Run Code Online (Sandbox Code Playgroud)

性能警告

内插字符串很慢。根据我的经验,这是顺序(从快到慢):

  1. value.ToString(format)+" blah blah"
  2. string.Format("{0:format} blah blah", value)
  3. $"{value:format} blah blah"


Jac*_*son 7

正如已经提到的,您将需要使用格式化结果;这都是通过Write()WriteLine()Format()ToString()方法完成的。

没有提到的是定点格式,它允许指定的小数位数。它使用“F”,“F”后面的数字是输出的小数位数,如示例所示。

Console.WriteLine("{0:F2}", 12);    // 12.00 - two decimal places
Console.WriteLine("{0:F0}", 12.3);  // 12 - ommiting fractions
Run Code Online (Sandbox Code Playgroud)


Sky*_*n R 6

我喜欢用

$"{value:0.##}
Run Code Online (Sandbox Code Playgroud)

如果有某个值,它可以选择显示小数。

例子:

$"{50.255:0.##} //50,25
$"{50.2:0.##}   //50,2
$"{50.00:0.##}  //50
Run Code Online (Sandbox Code Playgroud)


dan*_*iax 5

string outString= number.ToString("####0.00");
Run Code Online (Sandbox Code Playgroud)

  • 自定义格式中的“0”和“#”是有区别的。“0”:如果存在,则将零替换为相应的数字;否则,结果字符串中将出现零。“#”:如果“#”符号存在,则将其替换为相应的数字;否则,结果字符串中不会出现任何数字。[参考](https://www.quora.com/How-do-I-format-floating-point-numbers-in-C) (3认同)