[英]one decimal for string format
我的数字低于数字。 我想在小数点后显示一位数。 如何格式化?
2.85
2
1.99
我正在使用(“{0:0.0}”。但数据显示为
2.9 //It should be 2.8
2.0 //It should be 2
2.0 //It should be 1.9
尝试使用"{0:0.#}"
作为格式字符串。 但是,这只会修复.0
。 要将舍入修复为始终向下舍入,您可能需要使用:
string s = (Math.Floor(value * 10) / 10).ToString("0.#");
Decimal[] decimals = { new Decimal(2.85), new Decimal(2), new Decimal(1.99) };
foreach (var x in decimals)
{
Console.WriteLine(string.Format("{0:0.#}", Decimal.Truncate(x * 10) / 10));
}
// output
2.8
2
1.9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.