I want to do this using the Math.Round
function
问题:
回答1:
Here\'s some examples:
decimal a = 1.994444M;
Math.Round(a, 2); //returns 1.99
decimal b = 1.995555M;
Math.Round(b, 2); //returns 2.00
You might also want to look at bankers rounding / round-to-even with the following overload:
Math.Round(a, 2, MidpointRounding.ToEven);
There\'s more information on it here.
回答2:
Try this:
twoDec = Math.Round(val, 2)
回答3:
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.
回答4:
If you\'d like a string
> (1.7289).ToString(\"#.##\")
\"1.73\"
Or a decimal
> Math.Round((Decimal)x, 2)
1.73m
But remember! Rounding is not distributive, ie. round(x*y) != round(x) * round(y)
. So don\'t do any rounding until the very end of a calculation, else you\'ll lose accuracy.
回答5:
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:
// convert upto two decimal places
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\"
can also combine \"0\" with \"#\".
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\"
回答7:
I know its an old question but please note for the following differences between Math round and String format round:
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\"
回答8:
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)
回答9:
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.
回答10:
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.
回答11:
string a = \"10.65678\";
decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)
回答12:
public double RoundDown(double number, int decimalPlaces)
{
return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}
回答13:
Math.Floor(123456.646 * 100) / 100 Would return 123456.64