Hi All,
I would like to round off any number to 3 decimal places . I tried below methods but not giving the expected results,
sample = 14.23448 Method1 :
CDbl(sampl).ToString(“#.###”) gives 14.234 but the expected result is 14.235 since the last digit is 8.While converting, 4th decimal should be 5 and in turn the 3rd decimal be 5 Method 2:Same Problem
Math.Round(CDbl(sampl),3).ToString = 14.234 but the expected result is 14.235 Method 3: Same problem
CDbl(sampl).ToString(“#.000”) = 14.234 but the expected result is 14.235
You can use the Math.Round() method to achieve the desired result. However, you need to use the overload that takes the MidpointRounding parameter to specify how to handle rounding of numbers that are exactly halfway between two possible rounded values.
In your case, you can use MidpointRounding.AwayFromZero to ensure that rounding is always away from zero, so that numbers that are exactly halfway between two possible rounded values are rounded up.
Here’s an example:
Dim sample As Double = 14.23448
Dim roundedValue As Double = Math.Round(sample, 3, MidpointRounding.AwayFromZero)
The roundedValue variable will contain the value 14.235, which is the expected result.