3

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);
}
FrozenHaxor
  • 59
  • 1
  • 2
  • 8
  • 5
    How do you expect it to differentiate between 0.9999999 -> 1.00 and 0.66666666 -> 0.67? There is no standard rounding system which will handle those two cases separately. You either get 0.66 and 0.99 or you get 0.67 and 1.00. What if the number is 0.99 or 0.999? What should they do? – Bobson Aug 09 '13 at 18:46
  • I think the question was stated incorrectly and that 0.66666 should be displayed as 0.67. If the question was stated correctly, and 0.66666 really should be displayed as 0.67 then the OP needs to clarify what rule they want. If it's not round down or round-to-nearest then what is it? It is impossible to answer the question without knowing. But I think the question was just stated incorrectly. – Bruce Dawson Aug 02 '19 at 20:26

5 Answers5

9

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.

Trevor
  • 16,080
  • 9
  • 52
  • 83
  • 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. – Robert Harvey Aug 09 '13 at 18:55
  • 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?) – FrozenHaxor Aug 09 '13 at 21:32
  • Here is a reference showing how the toString method can be used on doubles. http://msdn.microsoft.com/en-us/library/3hfd35ad.aspx Can you paste a few lines of code? – Trevor Aug 09 '13 at 21:37
  • Well this code works well, but in situations where there is no decimal point in the double, it fails. Any solutions for that? – FrozenHaxor Aug 09 '13 at 21:44
  • 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 Aug 09 '13 at 21:47
  • Worked perfectly. Now everything works like I wanted! Thank you! – FrozenHaxor Aug 09 '13 at 21:54
6
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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
4
Math.Round((decimal)number, 2)

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

jbabey
  • 45,965
  • 12
  • 71
  • 94
3

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.

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
user270576
  • 987
  • 10
  • 16
1

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,938
  • 48
  • 39