Round off to nearest decimal

Hi all,

I need to round off number to nearest decimals. Can someone help here?

For eg : if I have 0.0000003456, I need to round that off to “0.0000003”
if I have 0.0046, I need to round that off to “00005”

I tried the following syntax, but it didn’t help -

string.Format(System.Globalization.CultureInfo.GetCultureInfo(“en-IN”),“{0:n}”,Math.Round(Convert.ToDouble(129454.92)))

Hi @Krithi1

CDbl(String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-IN"),"{0:n}",Math.Round(CDbl(129454.92))))

Note: Output DataType: System.Double
The above syntax explanation:

  1. System.Globalization.CultureInfo.GetCultureInfo("en-IN"): This part retrieves the culture information for the specified culture name “en-IN,” which represents the English (India) culture. Culture information affects how numbers, dates, and other types of data are formatted and displayed.
  2. "n": This is a format specifier for numeric values in the “Number” format. It represents a numeric value with a thousands separator and a fixed number of decimal places (usually two).
  3. Math.Round(CDbl(129454.92)): This part converts the given number (129454.92) into a Double data type using Convert.ToDouble and then applies the Math.Round function to round the number to the nearest integer. In this case, 129454.92 will be rounded to 129455.

so, if the number is 0.0046 and you apply the above syntax it will be formatted to 0
so you can use below syntax:

Math.Round(CDbl(input),4)

This is return the value 0.005

Hope it helps!!

1 Like

Hi,

How about the following?

dblVar = 0.0046

then

Double.Parse(System.Text.RegularExpressions.Regex.Replace(dblVar.ToString("E"), "^[.\d]+", Function(m) Math.Round(Double.Parse(m.Value),MidpointRounding.AwayFromZero).ToString()))

Regards,