Skip to content

Commit

Permalink
Add test and fix for Single as well, NightOwl888#128
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 31, 2024
1 parent de16c9b commit 6ebc51b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/J2N/Numerics/RyuSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,10 @@ private unsafe static void WriteBuffer(char* result, ref int index, int output,
result[index++] = upperCase ? 'E' : 'e';
if (exp < 0)
{
result[index++] = '-';
foreach (var nc in negSign)
{
result[index++] = nc;
}
exp = -exp;
}
if (exp >= 10)
Expand Down Expand Up @@ -651,4 +654,4 @@ private static int DecimalLength(int v)
return length;
}
}
}
}
21 changes: 21 additions & 0 deletions tests/NUnit/J2N.Tests/Numerics/TestSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3212,5 +3212,26 @@ protected override bool GetResult(string value, out float result)

#endregion TryParse_CharSequence_Single
}

// 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))
{
float f = 6.815791e-4f; // small value that gets formatted as exponential notation

string formatted = Single.ToString(f, culture);

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

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

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

0 comments on commit 6ebc51b

Please sign in to comment.