ghi-electronics / TinyCLR-Libraries

Official Libraries supporting TinyCLR OS
https://www.ghielectronics.com/tinyclr/
17 stars 16 forks source link

Incorrect Double.ToString formatting up to an thrown Exceptions #1330

Closed cdwork closed 6 months ago

cdwork commented 9 months ago

Original post "Incorrect Double.ToString formatting up to an thrown Exceptions"

Double.ToString using formatting throws an exception. For example:

(0.1).ToString("F3");                     // Work correct
(0.6-0.5).ToString("F3");                 // throw exception
(0.099999999999999978).ToString("F3");    // throw exception

because of var result = FormatNative(value, formatCh, precision); in System.Number.Format(object value, bool isInteger, string format, NumberFormatInfo info) will return a string, firstly, that does not match the value, and secondly, with “non-text” characters.

value = 0.099999999999999978
formatCh = "F"
precision = 3
result = "0.011000000000000000000000000000000000000000000000000 \b$] 0p0�a�0�aD"

different values reproduce exceptions with different specified formatting precision.

Palomino34 commented 9 months ago

link on forum: https://forums.ghielectronics.com/t/incorrect-double-tostring-formatting-up-to-an-thrown-exceptions/24796

Palomino34 commented 9 months ago

seem duplicate but need verify: https://github.com/ghi-electronics/TinyCLR-Libraries/issues/629