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

Revision en6, by keymoon, 2017-12-30 03:01:27

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 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}";
}

edit. This issue is not a bug. Thank you for teaching me AlexDmitriev, Gassa.

This is locale problem. I am using "Ja" locale, but I should use "en_US" locale.

Proof: 33796322 33796340

Tags c#, issues, locale, mono, ms

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English keymoon 2017-12-30 03:01:27 199
en5 English keymoon 2017-12-30 01:02:25 0 (published)
en4 English keymoon 2017-12-30 00:58:41 6
en3 English keymoon 2017-12-30 00:57:55 12
en2 English keymoon 2017-12-30 00:57:30 777
en1 English keymoon 2017-12-30 00:35:48 670 Initial revision (saved to drafts)