Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for rounding up in VB.NET

Tags:

vb.net

What is the syntax to round up a decimal leaving two digits after the decimal point?

Example: 2.566666 -> 2.57

like image 697
leonita Avatar asked May 19 '10 01:05

leonita


2 Answers

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
like image 72
Guffa Avatar answered Sep 16 '22 22:09

Guffa


Here is how I do it:

Private Function RoundUp(value As Double, decimals As Integer) As Double

    Return Math.Ceiling(value * (10 ^ decimals)) / (10 ^ decimals)

End Function
like image 23
Pierre Gauvin Avatar answered Sep 19 '22 22:09

Pierre Gauvin