How do you round a number to two decimal places in

2018-12-31 14:47发布

I want to do this using the Math.Round function

13条回答
几人难应
2楼-- · 2018-12-31 15:22

You should be able to specify the number of digits you want to round to using Math.Round(YourNumber, 2)

You can read more here.

查看更多
临风纵饮
3楼-- · 2018-12-31 15:27

This is for rounding to 2 decimal places in C#:

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

In VB.NET:

 Imports System.Math
 round(label8.text,2)
查看更多
骚的不知所云
4楼-- · 2018-12-31 15:31

Personally I never round anything. Keep it as resolute as possible, since rounding is a bit of a red herring in CS anyway. But you do want to format data for your users, and to that end, I find that string.Format("{0:0.00}", number) is a good approach.

查看更多
听够珍惜
5楼-- · 2018-12-31 15:32

Wikipedia has a nice page on rounding in general.

All .NET (managed) languages can use any of the common language run time's (the CLR) rounding mechanisms. For example, the Math.Round() (as mentioned above) method allows the developer to specify the type of rounding (Round-to-even or Away-from-zero). The Convert.ToInt32() method and its variations use round-to-even. The Ceiling() and Floor() methods are related.

You can round with custom numeric formatting as well.

Note that Decimal.Round() uses a different method than Math.Round();

Here is a useful post on the banker's rounding algorithm. See one of Raymond's humorous posts here about rounding...

查看更多
不再属于我。
6楼-- · 2018-12-31 15:33

string a = "10.65678";

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

查看更多
忆尘夕之涩
7楼-- · 2018-12-31 15:35

One thing you may want to check is the Rounding Mechanism of Math.Round:

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

Other than that, I recommend the Math.Round(inputNumer, numberOfPlaces) approach over the *100/100 one because it's cleaner.

查看更多
登录 后发表回答