【C#】Sometime double.ToString() returns weird value

Правка en3, от keymoon, 2017-12-30 00:57:55

Code 1.

using System;

class P
{
    static void Main()
    {
        double d = 1.23456;
        Console.WriteLine(d);
    }
}

Code 2.

using System;
class P
{
    static void Main()
    {
        double d = 1.23456;
        Console.WriteLine(d);
    }
}

Can you find difference between two?

Yes,It was JUST line feed. And,these two code lead to a different result.

Result 1.1,23456

Result 2.1.23456

It seems '.' replaced by ','.

The same problem occurs in Mono.

I think this probrem is bug,but I highly recommend to use substitute methods like this:

static string doubleToString(double d)
{
    int s = (int)d;
    string a = Convert.ToInt32((d - s) * 1000000000).ToString().PadLeft(9, '0');
    return $"{s}.{a}";
}

Proof: 33796322 33796340

Теги c#, issues, locale, mono, ms

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский keymoon 2017-12-30 03:01:27 199
en5 Английский keymoon 2017-12-30 01:02:25 0 (published)
en4 Английский keymoon 2017-12-30 00:58:41 6
en3 Английский keymoon 2017-12-30 00:57:55 12
en2 Английский keymoon 2017-12-30 00:57:30 777
en1 Английский keymoon 2017-12-30 00:35:48 670 Initial revision (saved to drafts)