【C#】Sometime double.ToString() returns weird value
Difference between en4 and en5, changed 0 character(s)
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:↵
[submission:33796322]↵
[submission:33796340]

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)