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


当前回答

我知道这是一个老问题,但请注意数学轮和字符串轮之间的以下区别:

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"

其他回答

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

试试这个:

twoDec = Math.Round(val, 2)

Math.Floor(123456.646 * 100) / 100 会返回123456.64吗

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

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

在VB。NET:

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

如果你想要一根绳子

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

或者小数

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

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