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


当前回答

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

其他回答

//转换到小数点后两位

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"

如果你想要一根绳子

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

或者小数

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

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

您应该能够使用Math指定要舍入的位数。轮(YourNumber, 2)

你可以在这里阅读更多。

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

试试这个:

twoDec = Math.Round(val, 2)