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


当前回答

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

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

在VB。NET:

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

其他回答

维基百科上有一个很好的四舍五入页面。

所有。net(托管)语言都可以使用任何公共语言运行时(CLR)舍入机制。例如,Math.Round()(如上所述)方法允许开发人员指定舍入的类型(舍入到偶数或远离零)。Convert.ToInt32()方法及其变体使用舍入到偶数。Ceiling()和Floor()方法是相关的。

您也可以使用自定义数字格式舍入。

注意Decimal.Round()使用了与Math.Round()不同的方法;

这是一篇关于银行家舍入算法的有用文章。 请看Raymond关于舍入的幽默帖子…

就我个人而言,我从来没有四舍五入过。让它尽可能坚定,因为舍入在CS中是一个转移注意力的东西。但您确实希望为用户格式化数据,为此,我发现string.Format("{0:0.00}", number)是一种很好的方法。

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

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"

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

//转换到小数点后两位

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"