我想使用Math.Round
函数
答案 0 :(得分:551)
以下是一些例子:
decimal a = 1.994444M;
Math.Round(a, 2); //returns 1.99
decimal b = 1.995555M;
Math.Round(b, 2); //returns 2.00
您可能还希望看看银行家四舍五入/四舍五入以下过载:
Math.Round(a, 2, MidpointRounding.ToEven);
关于它的更多信息here。
答案 1 :(得分:86)
试试这个:
twoDec = Math.Round(val, 2)
答案 2 :(得分:33)
就我个人而言,我从来没有做过任何事保持尽可能坚决,因为无论如何,舍入在CS中都是一个红色的鲱鱼。但您确实希望为用户设置数据格式,为此,我发现string.Format("{0:0.00}", number)
是一种很好的方法。
答案 3 :(得分:28)
如果你想要一个字符串
> (1.7289).ToString("#.##")
"1.73"
或小数
> Math.Round((Decimal)x, 2)
1.73m
但请记住!舍入不是分配的,即。 round(x*y) != round(x) * round(y)
。因此,在计算结束之前不要进行任何舍入,否则你将失去准确性。
答案 4 :(得分:13)
//最多转换两位小数
String.Format("{0:0.00}", 140.6767554); // "140.67"
String.Format("{0:0.00}", 140.1); // "140.10"
String.Format("{0:0.00}", 140); // "140.00"
Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2); // 140.67
decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2); // 140.67
======
// just two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
也可以将“0”与“#”组合在一起。
String.Format("{0:0.0#}", 123.4567) // "123.46"
String.Format("{0:0.0#}", 123.4) // "123.4"
String.Format("{0:0.0#}", 123.0) // "123.0"
答案 5 :(得分:12)
Wikipedia has a nice page关于四舍五入。
所有.NET(托管)语言都可以使用任何公共语言运行时(CLR)舍入机制。例如,Math.Round()(如上所述)方法允许开发人员指定舍入类型(Round-to-even或Away-from-zero)。 Convert.ToInt32()方法及其变体使用round-to-even。 Ceiling()和Floor()方法相关。
您也可以使用custom numeric formatting进行回合。
请注意Decimal.Round()使用的方法与Math.Round();
不同这是银行家舍入算法的useful pos t。 请参阅雷蒙德关于四舍五入的幽默posts here之一......
答案 6 :(得分:6)
我知道这是一个老问题,但请注意数学回合和字符串格式回合之间的以下差异:
decimal d1 = (decimal)1.125;
Math.Round(d1, 2).Dump(); // returns 1.12
d1.ToString("#.##").Dump(); // returns "1.13"
decimal d2 = (decimal)1.1251;
Math.Round(d2, 2).Dump(); // returns 1.13
d2.ToString("#.##").Dump(); // returns "1.13"
答案 7 :(得分:5)
这是用于在C#中舍入到2个小数位:
label8.Text = valor_cuota .ToString("N2") ;
在VB.NET中:
Imports System.Math
round(label8.text,2)
答案 8 :(得分:4)
您可能想要检查的一件事是Math.Round的舍入机制:
http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx
除此之外,我推荐使用Math.Round(inputNumer,numberOfPlaces)方法而不是* 100/100,因为它更干净。
答案 9 :(得分:3)
您应该可以使用Math.Round(YourNumber,2)指定要舍入的位数
您可以阅读更多here。
答案 10 :(得分:1)
字符串a =“10.65678”;
十进制d = Math.Round(Convert.ToDouble(a.ToString()),2)
答案 11 :(得分:0)
public double RoundDown(double number, int decimalPlaces)
{
return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}
答案 12 :(得分:0)
Math.Floor(123456.646 * 100)/ 100 将返回123456.64
答案 13 :(得分:0)
如果要四舍五入,可以根据以下方式获得不同的结果:使用Math.Round()函数的方式(如果是向上舍入或向下舍入),则使用双精度和/或浮点数字,然后应用中点舍入。特别是,在其内部使用操作或要舍入的变量来自操作时。假设您要将这两个数字相乘: 0.75 * 0.95 = 0.7125 。对?不在C#中
让我们看看如果要四舍五入到小数点后第三位会发生什么:
double result = 0.75d * 0.95d; // result = 0.71249999999999991
double result = 0.75f * 0.95f; // result = 0.71249997615814209
result = Math.Round(result, 3, MidpointRounding.ToEven); // result = 0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = 0.712. Should be 0.713
如您所见,如果要舍入中点,则第一个Round()是正确的。但是第二个Round(),如果您想四舍五入,那就错了。
这适用于负数:
double result = -0.75 * 0.95; //result = -0.71249999999999991
result = Math.Round(result, 3, MidpointRounding.ToEven); // result = -0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = -0.712. Should be -0.713
因此,恕我直言,您应该为Math.Round()创建适合您需求的自己的包装函数。我创建了一个函数,参数'roundUp = true'表示舍入到下一个更大的数字。即:0.7125舍入为0.713,-0.7125舍入为-0.712(因为-0.712> -0.713)。这是我创建的函数,适用于任意数量的小数:
double Redondea(double value, int precision, bool roundUp = true)
{
if ((decimal)value == 0.0m)
return 0.0;
double corrector = 1 / Math.Pow(10, precision + 2);
if ((decimal)value < 0.0m)
{
if (roundUp)
return Math.Round(value, precision, MidpointRounding.ToEven);
else
return Math.Round(value - corrector, precision, MidpointRounding.AwayFromZero);
}
else
{
if (roundUp)
return Math.Round(value + corrector, precision, MidpointRounding.AwayFromZero);
else
return Math.Round(value, precision, MidpointRounding.ToEven);
}
}
变量'corrector'用于修复浮点数或双数运算的不准确性。
答案 14 :(得分:0)
发生一种奇怪的情况,我有一个十进制变量,当序列化55.50时,它总是在数学上将默认值设置为55.5。但是,由于某些原因,我们的客户系统严重期望55.50,因此他们肯定期望十进制。那就是我编写以下帮助程序时所使用的帮助程序,它总是将填充的任何十进制值转换为带有零的两位,而不是发送字符串。
public static class DecimalExtensions
{
public static decimal WithTwoDecimalPoints(this decimal val)
{
return decimal.Parse(val.ToString("0.00"));
}
}
用法应该
var sampleDecimalValueV1 = 2.5m;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());
decimal sampleDecimalValueV1 = 2;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());
输出:
2.50
2.00