Skip to content

Commit

Permalink
Fix issue with non-hyphen negative signs in exp notation, NightOwl888…
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 31, 2024
1 parent 8845a69 commit de16c9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/J2N/Numerics/RyuDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,11 @@ private unsafe static void WriteBuffer(char* result, ref int index, long output,
result[index++] = upperCase ? 'E' : 'e';
if (exp < 0)
{
result[index++] = '-';
foreach (var nc in negSign)
{
result[index++] = nc;
}

exp = -exp;
}
if (exp >= 100)
Expand Down
21 changes: 21 additions & 0 deletions tests/NUnit/J2N.Tests/Numerics/TestDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4583,5 +4583,26 @@ protected override bool GetResult(string value, out double result)
#endregion TryParse_CharSequence_Double

}

// J2N specific - GitHub issue #128
[Test]
public void Issue128_UnicodeMinusSignExponentialNotation()
{
// Some cultures (such as sv-FI) use a Unicode minus sign (U+2212) instead of the ASCII hyphen-minus (U+002D) as the negative sign.
// Previously, the formatter was using a hyphen as the negative sign, which caused parsing to fail when the culture used a different character.
// This test verifies that the parser can handle Unicode minus signs in exponential notation, as well as i.e. decimal commas.
foreach (var culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
double d = 6.815791e-4d; // small value that gets formatted as exponential notation

string formatted = Double.ToString(d, culture);

assertEquals($"6{culture.NumberFormat.NumberDecimalSeparator}815791E{culture.NumberFormat.NegativeSign}4", formatted);

double parsed = Double.Parse(formatted, culture);

assertEquals(d, parsed, 0.1e-10d);
}
}
}
}

0 comments on commit de16c9b

Please sign in to comment.