© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
4 replies
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ how to round numbers like real math
C#CC# / help
3y ago
math
C#CC# / help
3y ago
✅ C# Academy Math Game Issues
C#CC# / help
3y ago