Round double in two decimal places in C#?

C#DoubleRounding

C# Problem Overview


I want to round up double value in two decimal places in c# how can i do that?

double inputValue = 48.485;

after round up

inputValue = 48.49;

###Related: https://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa

C# Solutions


Solution 1 - C#

This works:

inputValue = Math.Round(inputValue, 2);

Solution 2 - C#

Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)

Solution 3 - C#

Another easy way is to use ToString with a parameter. Example:

float d = 54.9700F;    
string s = d.ToString("N2");
Console.WriteLine(s);

Result:

54.97

Solution 4 - C#

You should use

inputvalue=Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)

Math.Round > Math.Round rounds a double-precision floating-point value to a > specified number of fractional digits.

MidpointRounding

> Specifies how mathematical rounding methods should process a number > that is midway between two numbers.

Basically the function above will take your inputvalue and round it to 2 (or whichever number you specify) decimal places. With MidpointRounding.AwayFromZero when a number is halfway between two others, it is rounded toward the nearest number that is away from zero. There is also another option you can use that rounds towards the nearest even number.

Solution 5 - C#

Use Math.Round

value = Math.Round(48.485, 2);

Solution 6 - C#

you can try one from below.there are many way for this.

1. 
 value=Math.Round(123.4567, 2, MidpointRounding.AwayFromZero) //"123.46"
2.
 inputvalue=Math.Round(123.4567, 2)  //"123.46"
3. 
 String.Format("{0:0.00}", 123.4567);      // "123.46"
4. 
string.Format("{0:F2}", 123.456789);     //123.46
string.Format("{0:F3}", 123.456789);     //123.457
string.Format("{0:F4}", 123.456789);     //123.4568

Solution 7 - C#

Use an interpolated string, this generates a rounded up string:

var strlen = 6;
$"{48.485:F2}"

Output

"48.49"

Solution 8 - C#

I think all these answers are missing the question. The problem was to "Round UP", not just "Round". It is my understanding that Round Up means that ANY fractional value about a whole digit rounds up to the next WHOLE digit. ie: 48.0000000 = 48 but 25.00001 = 26. Is this not the definition of rounding up? (or have my past 60 years in accounting been misplaced?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsanjeev40084View Question on Stackoverflow
Solution 1 - C#Alex LEView Answer on Stackoverflow
Solution 2 - C#nandinView Answer on Stackoverflow
Solution 3 - C#DiwasView Answer on Stackoverflow
Solution 4 - C#GageView Answer on Stackoverflow
Solution 5 - C#recursiveView Answer on Stackoverflow
Solution 6 - C#reza.cse08View Answer on Stackoverflow
Solution 7 - C#0x777View Answer on Stackoverflow
Solution 8 - C#Bobby D.View Answer on Stackoverflow