289

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: c# - How do I round a decimal value to 2 decimal places (for output on a page)

Community
  • 1
  • 1
sanjeev40084
  • 7,999
  • 17
  • 62
  • 93
  • Take note that you can't represent all floating point values using double and this can [trip you up when rounding](https://stackoverflow.com/q/27323465/542251) – Liam Apr 15 '19 at 10:36

8 Answers8

529

This works:

inputValue = Math.Round(inputValue, 2);
Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Alex LE
  • 17,974
  • 4
  • 28
  • 28
  • workaround to trunc float: float myTruncFloat = float.Parse(Math.Round(myFloat, 2).ToString()); – Piero Alberto Jun 01 '16 at 12:24
  • 3
    If value is 48.0000. It will not result 48.00. double inputValue = 48.00; inputValue = Math.Round(inputValue, 2); will result 48 only. Any warkaound ? – user1926138 Jul 26 '18 at 11:35
  • according to the documentation this would round mid point numbers to the nearest even number https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=netcore-3.1#System_Math_Round_System_Double_System_Int32_ – rdans Sep 08 '20 at 12:17
114
Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)
nandin
  • 2,409
  • 5
  • 22
  • 26
  • 4
    This is actually what should be used. Most operations in banks etc are done using this method (MidpointRounding.AwayFromZero). – MadBoy Mar 01 '10 at 18:40
  • 45
    suggesting that banks use doubles is scary dude, don't use approximation values for currency. – raggi Sep 28 '11 at 17:38
  • 6
    @raggi I'm afraid some (banks) do... That would explain a lot of stuff – SparK Nov 11 '13 at 15:55
  • 4
    Yes I believe this was what the movie 'Office Space' was made after - rounding off fractions of cents that nobody would notice. Good point to be careful about when to arbitrarily use rounding. – atconway Sep 06 '14 at 16:22
  • 3
    And as part of the plot of one of the original Superman movies waaay long time ago. – James Westgate May 05 '17 at 10:39
  • 1
    @JamesWestgate "They did it in Superman 3" – Brady Moritz Jan 23 '21 at 18:52
29

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
Diwas
  • 573
  • 5
  • 11
26

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.

Gage
  • 7,067
  • 9
  • 44
  • 77
21

Use Math.Round

value = Math.Round(48.485, 2);
recursive
  • 77,417
  • 29
  • 137
  • 228
  • Beat me to it (although I'd add a semi-colon on there, too ;) ) – Reed Copsey Mar 01 '10 at 17:53
  • 1
    However, be careful with MidpointRounding: "If the value of the first digit in value to the right of the digits decimal position is 5, the digit in the digits position is rounded up if it is odd, or left unchanged if it is even" – Matthias Mar 01 '10 at 17:54
9

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
reza.cse08
  • 5,172
  • 38
  • 34
3

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

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

Output

"48.49"
bigpony
  • 617
  • 5
  • 12
1

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?

Bobby D.
  • 11
  • 2