what is the syntax to round up a decimal leaving 2 digits after the decimal point, ex: 2.566666 -> 2.57
A:
You can use System.Math
, specifically Math.Round()
, like this:
Math.Round(2.566666, 2)
Nick Craver
2010-05-19 01:26:22
+1
A:
Math.Round is what you're looking for. If you're new to rounding in .Net - you should also look up the difference between AwayFromZero and ToEven rounding. The default of ToEven can sometime take people by surprise.
dim result = Math.Round(2.56666666, 2)
Scott Ivey
2010-05-19 01:26:54
ah..yeahh...thx
leonita
2010-05-19 01:32:22
A:
Math.Round()
, as suggested by others, is probably what you want. But the text of your question specifically asked how to "roundup"[sic]. If you always need to round up, regarless of actual value (ie: 2.561111 would still go to 2.57), you can do this:
Math.Ceiling(d * 100)/100D
Joel Coehoorn
2010-05-19 01:29:46
A:
If you want regular rounding, you can just use the Math.Round
method. If you specifially want to round upwards, you use the Math.Ceiling
method:
Dim d As Decimal = 2.566666
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Guffa
2010-05-19 01:32:11