tags:

views:

42

answers:

4

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
+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
ah..yeahh...thx
leonita
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
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