C# 将逗号后的两位数格式化为两位数,无需四舍五入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18153559/
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
Format a double to two digits after the comma without rounding up or down
提问by FrozenHaxor
I have been searching forever and I simply cannot find the answer, none of them will work properly.
我一直在寻找,我根本找不到答案,它们都不能正常工作。
I want to turn a double like 0.33333333333 into 0,33 or 0.6666666666 into 0,66
我想把像 0.33333333333 这样的双精度数变成 0,33 或 0.6666666666 变成 0,66
Number like 0.9999999999 should become 1 though.
不过,像 0.9999999999 这样的数字应该变成 1。
I tried various methods like
我尝试了各种方法,例如
value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture)
It just returns garbage or rounds the number wrongly. Any help please?
它只是返回垃圾或错误地舍入数字。请问有什么帮助吗?
Basically every number is divided by 9, then it needs to be displayed with 2 decimal places without any rounding.
基本上每个数字都除以9,那么就需要显示2位小数,没有任何四舍五入。
I have found a nice function that seems to work well with numbers up to 9.999999999 Beyond that it starts to lose one decimal number. With a number like 200.33333333333 its going to just display 200 instead of 200,33. Any fix for that guys?
我发现了一个很好的函数,它似乎可以很好地处理高达 9.999999999 的数字,除此之外它开始丢失一个十进制数字。对于像 200.33333333333 这样的数字,它只会显示 200 而不是 200,33。对那些家伙有什么解决办法吗?
Here it is:
这里是:
string Truncate(double value, int precision)
{
string result = value.ToString();
int dot = result.IndexOf(',');
if (dot < 0)
{
return result;
}
int newLength = dot + precision + 1;
if (newLength == dot + 1)
{
newLength--;
}
if (newLength > result.Length)
{
newLength = result.Length;
}
return result.Substring(0, newLength);
}
采纳答案by Trevor
Have you tried
你有没有尝试过
Math.Round(0.33333333333, 2);
Update*
更新*
If you don't want the decimal rounded another thing you can do is change the double to a string and then get get a substring to two decimal places and convert it back to a double.
如果您不希望小数四舍五入,您可以做的另一件事是将双精度值更改为字符串,然后将子字符串转换为两位小数并将其转换回双精度值。
doubleString = double.toString();
if(doubleString.IndexOf(',') > -1)
{
doubleString = doubleString.Substring(0,doubleString.IndexOf(',')+3);
}
double = Convert.ToDouble(doubleString);
You can use a if statement to check for .99 and change it to 1 for that case.
您可以使用 if 语句来检查 0.99 并将其更改为 1。
回答by Robert Harvey
Math.Truncate(value * 100)/100
Although I make no guarantees about how the division will affect the floating point number. Decimal numbers can often not be represented exactly in floating point, because they are stored as base 2, not base 10, so if you want to guarantee reliability, use a decimal
, not a double
.
虽然我不保证除法将如何影响浮点数。十进制数通常不能精确地用浮点数表示,因为它们以 2 为基数存储,而不是以 10 为基数存储,因此如果要保证可靠性,请使用 adecimal
而不是 a double
。
回答by jbabey
Math.Round((decimal)number, 2)
Casting to a decimal first will avoid the precision issues discussed on the documentation page.
首先转换为小数将避免文档页面上讨论的精度问题。
回答by user270576
Math.Floor
effectively drops anything after the decimal point. If you want to save two digits, do the glitch operation- multiply then divide:
Math.Floor
有效地删除小数点后的任何内容。如果要保存两位数,请执行故障操作- 乘以除法:
Math.Floor(100 * number) / 100
This is faster and safer than doing a culture-dependent search for a comma in a double-converted-to-string, as accepted answer suggests.
正如公认的答案所暗示的那样,这比在双重转换为字符串中对逗号进行依赖于文化的搜索更快、更安全。
回答by reza.cse08
you can try one from below.there are many way for this.
你可以从下面尝试一个。有很多方法可以做到这一点。
1.
value=Math.Round(123.4567, 2, MidpointRounding.AwayFromZero) //"123.46"
2.
inputvalue=Math.Round(123.4567, 2) //"123.46"
3.
String.Format("{0:0.00}", 123.4567); // "123.46"
4.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568