none
how formatting a number without rounding RRS feed

  • Question

  • hi, i have a double as 0.99999998000000001.

    i want to use double.ToString and get the number with 2 decimal digits ("0.99"), but the problem it's that by default the method ToString
    rounds the number to the last visible decimal digits
    (in my example the output string is "1.00").

    1 - there's some arguments of ToString to make it no rounding?

    2- and somebody know why microsoft developert have set this strange behaviour???
    i think that the most natural way to display a rounded number is to round the number and then display it, not to have the rounding behaviour in the tostring method
    Wednesday, July 9, 2008 12:54 PM

Answers

  •  
          double d = 0.99999998000000001;  
          d = Math.Floor(100 * d) / 100;  
          string s = d.ToString("N2");  
          Console.WriteLine(s);  
     

    Output: 0.99.  Asking MSFT to modify double.ToString() to truncate instead of round is going to be a mighty tough sale.
    Hans Passant.
    • Proposed as answer by Tigerwood2006 Thursday, July 10, 2008 12:34 AM
    • Marked as answer by developer00 Thursday, July 10, 2008 10:05 AM
    Wednesday, July 9, 2008 3:18 PM
    Moderator

All replies

  • If you are using VB.NET then you can use FormatNumber Function.
    Gaurav Khanna
    Wednesday, July 9, 2008 2:15 PM
  • no, i use c# :(
    Wednesday, July 9, 2008 3:16 PM
  •  
          double d = 0.99999998000000001;  
          d = Math.Floor(100 * d) / 100;  
          string s = d.ToString("N2");  
          Console.WriteLine(s);  
     

    Output: 0.99.  Asking MSFT to modify double.ToString() to truncate instead of round is going to be a mighty tough sale.
    Hans Passant.
    • Proposed as answer by Tigerwood2006 Thursday, July 10, 2008 12:34 AM
    • Marked as answer by developer00 Thursday, July 10, 2008 10:05 AM
    Wednesday, July 9, 2008 3:18 PM
    Moderator
  • Hi korkless,

    I agree it would be useful if ToString() had a format that allowed one to get a truncated string.  It's something I have to do in code, and something I have to do quite often.

    I thnk their default makes sense as the number it produces is the number that's closest to the orignal (0.99999998 is much closer to 1.00 than to 0.99).

    Also I think rounding has be done in ToString as you can't round unless you know how many decimal places you have.

    But if it can be done with VB's FormatNumber then you can access it by adding a referene to Microsoft.VisualBasic.

    If you start a petition for Microsoft to suport num.ToString("T2") I'll sign it.

    Good luck,

    John

    namespace Green 
        using Microsoft.VisualBasic; 
     
        class Grass 
        { 
            public static void Main() 
            { 
                double dub = 0.99999998000000001; 
                Console.WriteLine("my trunc:"); 
                string trunc = ((trunc = dub.ToString("f16")).Substring(0, trunc.Length - 14)); 
                Console.WriteLine(trunc); 
     
                Console.WriteLine(); 
                Console.WriteLine("can ue VB, but how?"); 
                string s; 
                s = Strings.FormatNumber(dub, 2, TriState.UseDefault, TriState.UseDefault, TriState.UseDefault); 
                Console.WriteLine(s); 
     
                Console.WriteLine(); 
                Console.WriteLine("can't round till you know how many place:"); 
                double dub2 = 0.999555; 
                for (int places = 0; places < 9; places++) 
                    Console.WriteLine(dub2.ToString("f" + places.ToString())); 
            } 
        } 
     
    //  Output: 
    // 
    //  my trunc: 
    //  0.99 
    // 
    //  can ue VB, but how? 
    //  1.00 
    // 
    //  can't round till you know how many place: 
    //  1 
    //  1.0 
    //  1.00 
    //  1.000 
    //  0.9996 
    //  0.99956 
    //  0.999555 
    //  0.9995550 
    //  0.99955500 
    // 
    //  Done. 
     



    • Edited by rtizan[0] Wednesday, July 9, 2008 7:15 PM typo
    Wednesday, July 9, 2008 5:32 PM
  • thanks for your messages, for the moment i will use Trunk but i don't like much as solution.

    the last 2 question:

    where i can add a petition for the new format?

    (external question) in vs Immediate Window whene i declare a double will be print his value (it's a general feature that works with all the classes/struct), that sring is not taken with default ToString method, somebody knows exactly how is calculate that value?
    (for example double d = 16.6 will print the value "16.600000000000001")
    Thursday, July 10, 2008 10:04 AM
  • It's Very Helpful
    Wednesday, January 2, 2013 7:50 AM