Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with non-hyphen negative signs in exp notation, #128 #129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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/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);
}
}
}
}
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);
}
}
}
}