C#C
C#16mo ago
DragonA.

Math.Round issue

I have strange issue with Math.Round in a first example I use it to round a double after calculation, and the output wrong, because it has to have 2 digits after dot, but the output have more digits after dot.

string input = Console.ReadLine();
double r = double.Parse(input);
const double pi = 3.14;
double volume = 4 * pi * r * r * r / 3;
Math.Round(volume, 2);
Console.WriteLine($"volume: {volume}");

r = 2
volume: 33,49333333333333

In a second example I round first of all round the result and then give it to the variable and all working right.

string input = Console.ReadLine();
double r = double.Parse(input);
const double pi = 3.14;
double volume = Math.Round(4 * pi * r * r * r / 3, 2);
Console.WriteLine($"volume: {volume}");

r = 2
volume: 33,49

Why is it happen like that?
Was this page helpful?