Format a double to two digits after the comma without rounding up or down

32,698

Solution 1

Have you tried

Math.Round(0.33333333333, 2);

Update*

If you don't want the decimal rounded another thing you can do is change the double to a string and then get get a substring to two decimal places and convert it back to a double.

doubleString = double.toString();
if(doubleString.IndexOf(',') > -1)
{
   doubleString = doubleString.Substring(0,doubleString.IndexOf(',')+3);
}
double = Convert.ToDouble(doubleString);

You can use a if statement to check for .99 and change it to 1 for that case.

Solution 2

Math.Truncate(value * 100)/100

Although I make no guarantees about how the division will affect the floating point number. Decimal numbers can often not be represented exactly in floating point, because they are stored as base 2, not base 10, so if you want to guarantee reliability, use a decimal, not a double.

Solution 3

Math.Round((decimal)number, 2)

Casting to a decimal first will avoid the precision issues discussed on the documentation page.

Solution 4

Math.Floor effectively drops anything after the decimal point. If you want to save two digits, do the glitch operation - multiply then divide:

Math.Floor(100 * number) / 100

This is faster and safer than doing a culture-dependent search for a comma in a double-converted-to-string, as accepted answer suggests.

Solution 5

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
Share:
32,698
FrozenHaxor
Author by

FrozenHaxor

Updated on October 28, 2020

Comments

  • FrozenHaxor
    FrozenHaxor over 3 years

    I have been searching forever and I simply cannot find the answer, none of them will work properly.

    I want to turn a double like 0.33333333333 into 0,33 or 0.6666666666 into 0,66

    Number like 0.9999999999 should become 1 though.

    I tried various methods like

    value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture)
    

    It just returns garbage or rounds the number wrongly. Any help please?

    Basically every number is divided by 9, then it needs to be displayed with 2 decimal places without any rounding.

    I have found a nice function that seems to work well with numbers up to 9.999999999 Beyond that it starts to lose one decimal number. With a number like 200.33333333333 its going to just display 200 instead of 200,33. Any fix for that guys?

    Here it is:

    string Truncate(double value, int precision)
    {
        string result = value.ToString();
    
        int dot = result.IndexOf(',');
        if (dot < 0)
        {
            return result;
        }
    
        int newLength = dot + precision + 1;
    
        if (newLength == dot + 1)
        {
            newLength--;
        }
    
        if (newLength > result.Length)
        {
            newLength = result.Length;
        }
    
        return result.Substring(0, newLength);
    }
    
  • Bobson
    Bobson over 10 years
    +1. Nice simple answer, although it doesn't handle the "0.9999 should become 1" special case.
  • Yinda Yin
    Yinda Yin over 10 years
    For the .9999999999 special case, I would simply add a small amount to the number to push it above 1.
  • Yinda Yin
    Yinda Yin over 10 years
    This will turn .666666666 into .67, which the OP specifically stated he didn't want. Round will turn .9999999999 into 1 anyway, so the special case would not be needed.
  • Yinda Yin
    Yinda Yin over 10 years
    This will turn .666666666 into .67, which the OP specifically stated he didn't want.
  • jbabey
    jbabey over 10 years
    @RobertHarvey that makes no sense though! :P
  • Yinda Yin
    Yinda Yin over 10 years
    Hey, I didn't write the requirements. :)
  • FrozenHaxor
    FrozenHaxor over 10 years
    It won't convert to string, I get error 'double' does not contain a definition for 'toString' and no extension method 'toString' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference?)
  • Trevor
    Trevor over 10 years
    Here is a reference showing how the toString method can be used on doubles. msdn.microsoft.com/en-us/library/3hfd35ad.aspx Can you paste a few lines of code?
  • FrozenHaxor
    FrozenHaxor over 10 years
    Well this code works well, but in situations where there is no decimal point in the double, it fails. Any solutions for that?
  • FrozenHaxor
    FrozenHaxor over 10 years
    Thank you so much for your time, also I needed to change +2 to +3 in order to get 2 numbers after the decimal point. I also changed the dot to a comma and it looks like this now: valueString.Substring(0, valueString.IndexOf(',') + 3)
  • FrozenHaxor
    FrozenHaxor over 10 years
    Worked perfectly. Now everything works like I wanted! Thank you!