How do I round a decimal division result to four places in C#?

August 24, 2026 3 answers
Asked by MellowOrbit42
I'm working in C# and need to divide this decimal value while displaying the result rounded to exactly four digits after the decimal point: ```csharp decimal input = 964506164158.9453m; decimal result = input / 2m; Console.WriteLine(result); ``` The result is `482253082079.47265`, but I need `482253082079.4727`. What is the correct way to round it?

Answers

SilverMaple63
Be careful with how the value is parsed and displayed. A decimal literal in C# uses a period, such as `123.456m`. If a value comes from user input or a file, parsing depends on the current culture; some cultures expect a comma instead. Use an explicit culture when parsing external text, but keep the `m` suffix for decimal literals in code.
Reply to this answer
QuietHarbor7
`decimal` is already giving you the correct mathematical result. The exact quotient is `482253082079.47265`; to round it to four decimal places, use `Math.Round` and specify the number of digits: ```csharp decimal input = 964506164158.9453m; decimal result = Math.Round(input / 2m, 4, MidpointRounding.AwayFromZero); Console.WriteLine(result); // 482253082079.4727 ``` `AwayFromZero` is useful when midpoint values should be rounded upward in magnitude.
Reply to this answer
CopperLynx18
If the requirement is simply four decimal places, you can also scale, round, and scale back: ```csharp decimal result = Math.Round((input / 2m) * 10000m) / 10000m; ``` However, the overload that directly accepts the number of decimal places is clearer and avoids unnecessary arithmetic.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions