[英]Formatting a float to 2 decimal places
我目前正在为客户网站构建销售模块。 到目前为止,我已经得到了完美计算的销售价格,但我遇到的问题是将输出格式化为小数点后 2 位。
我目前在一个变量中调用它,以便我可以将结果数据绑定到列表视图。
Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())),
谁能告诉我如何将输出格式化为小数点后 2 位? 非常感谢!
您可以将格式传递给ToString
方法,例如:
myFloatVariable.ToString("0.00"); //2dp Number
myFloatVariable.ToString("n2"); // 2dp Number
myFloatVariable.ToString("c2"); // 2dp currency
您需要做的第一件事是使用decimal
类型而不是float
作为价格。 使用float
是绝对不能接受的,因为它不能准确地表示大多数小数。
完成此操作后,可以使用Decimal.Round()
舍入到 2 个位置。
String.Format("{0:#,###.##}", value)
来自C# 中的字符串格式的更复杂示例:
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);
如果通过 1243.50,这将输出“$1,240.00”。 如果数字为负数,它将输出相同的格式但在括号中,如果数字为零,它将输出字符串“Zero”。
这适用于您想要使用内插字符串的情况。 我实际上发布了这个,因为我厌倦了反复试验,并且每次我需要格式化一些标量时最终都会滚动浏览大量文档。
$"{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" :)
性能警告
内插字符串很慢。 以我的经验,这是顺序(从快到慢):
value.ToString(format)+" blah blah"
string.Format("{0:format} blah blah", value)
$"{value:format} blah blah"
如前所述,您将需要使用格式化的结果; 这都是通过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
string outString= number.ToString("####0.00");
我喜欢用
$"{value:0.##}
只有在那些地方有一些值时,它才会显示两位小数。
例子:
$"{50.255:0.##} //50,25
$"{50.2:0.##} //50,2
$"{50.00:0.##} //50
private float LimitDecimalPlace(double number,int limitPlace)
{
float result = 0;
string sNumber = number.ToString();
int decimalIndex = sNumber.IndexOf(".");
if (decimalIndex != -1)
{
sNumber = sNumber.Remove(decimalIndex + limitPlace + 1);
}
result = float.Parse(sNumber);
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.