Skip to content

Commit

Permalink
(stdlib) Use more predictable format in float and double conversi…
Browse files Browse the repository at this point in the history
…on to string
  • Loading branch information
perlun committed Oct 30, 2023
1 parent 50b1ff9 commit 6b3e6d0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 74 deletions.
2 changes: 2 additions & 0 deletions release-notes/v0.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#### Data types
- Implement `AsciiString` for ASCII-only strings [[#377][377]]
- Implement `Utf8String` for strings with non-ASCII content [[#385][385]]
- Use more predictable format in `float` and `double` conversion to string [[#414][414]]

#### General
- Enable nullability checks for the `Program` class [[#386][386]]
Expand Down Expand Up @@ -41,3 +42,4 @@
[410]: https://github.com/perlang-org/perlang/pull/410
[412]: https://github.com/perlang-org/perlang/pull/412
[413]: https://github.com/perlang-org/perlang/pull/413
[414]: https://github.com/perlang-org/perlang/pull/414
10 changes: 10 additions & 0 deletions src/Perlang.Stdlib/Internal/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public static String Stringify(object @object)
{
return String.from(clrString);
}
else if (@object is float f)
{
// Us an explicit format, so we can produce something equal in the C# and C++/fmt-based implementations
return String.from(f.ToString("G7", CultureInfo.InvariantCulture));
}
else if (@object is double d)
{
// Us an explicit format, so we can produce something equal in the C# and C++/fmt-based implementations
return String.from(d.ToString("G15", CultureInfo.InvariantCulture));
}
else if (@object is IConvertible convertible)
{
// The explicit IFormatProvider is required to ensure we use 123.45 format, regardless of
Expand Down
Loading

0 comments on commit 6b3e6d0

Please sign in to comment.