我想用数学来做。圆的函数


当前回答

//转换到小数点后两位

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"

其他回答

这是为了在c#中四舍五入到小数点后2位:

label8.Text = valor_cuota .ToString("N2") ;

在VB。NET:

 Imports System.Math
 round(label8.text,2)

你可能需要检查的一件事是数学的舍入机制。轮:

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

除此之外,我推荐数学。Round(inputnumber, numberOfPlaces)接近*100/100,因为它更干净。

  public double RoundDown(double number, int decimalPlaces)
        {
            return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
        }

下面是一些例子:

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);

这里有更多的信息。

如果你想要一根绳子

> (1.7289).ToString("#.##")
"1.73"

或者小数

> Math.Round((Decimal)x, 2)
1.73m

但请记住!舍入不是分配的。圆(x*y) !=圆(x) *圆(y)。所以在计算结束之前不要做任何舍入运算,否则会失去精度。