How to round to a certain decimal precision?

Possible duplicate:
Radius of doubling to 2 significant digits after the decimal point

I have a meaning var i = 0.69999980926513672. I need to round this value to 0.7, is there a built-in method that will do this?

+3
source share
5 answers

You are looking for a method Math.Round.

//first param is number to round
//second param is the accuracy to use in the rounding (number of decimal places)
Math.Round(i, 2)
+3
source

Use one of:

System.Math.Round (i, 1, MidpointRounding.ToEven);
System.Math.Round (i, 1, MidpointRounding.AwayFromZero);

The difference is how it handles numbers that are equidistant to a rounded point (for example, 0.65 in your case can either go up to 0.7 or 0.6).

Here is the answer I gave to another question that contains much more information.

+13
source
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));

-

wow, 5 !

+3
source

Use the Math.Round method :

double i = 0.69999980926513672;
double result = Math.Round(i, 2);
+1
source

Source: https://habr.com/ru/post/1717118/


All Articles