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

Standard "F" and "N" numeric string-formatting specifiers should mention the rounding method used #8160

Closed
mklement0 opened this Issue Oct 8, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@mklement0

mklement0 commented Oct 8, 2018

Issue description

Using string formatting, e.g. via String.Format(), with the F and N standard format specifiers applies rounding to the numeric input as needed in order to limit the resulting "number string" to the requested or implied number of decimal places.

(Other formats may be affected as well - haven't looked at them systematically.)

The specific rounding method - which appears to be half-away-from-zero - should be mentioned in the docs, especially since the method differs from the half-to-even rounding applied by default by Math.Round().

E.g.:

      double n = 2.5;
      // String formatting.
      Console.WriteLine($"{n:F0}"); // -> '3' - half-away-from-zero rounding
      Console.WriteLine($"{n:N0}"); // -> '3' - ditto
      // To-integer type conversion.
      Console.WriteLine($"{Math.Round(n)}"); // -> '2'(!) - half-to-even rounding

Target framework

Check the .NET target framework(s) being used, and include the version number(s).

  • .NET Core
  • .NET Framework
  • .NET Standard

If using the .NET Core SDK, include dotnet --info output. If using .NET Framework without the .NET Core SDK, include info from Visual Studio's Help > About Microsoft Visual Studio dialog.

dotnet --info output or About VS info
.NET Core SDK 2.1.302

---

Microsoft .NET Framework Version 4.7.03056
@mklement0

This comment has been minimized.

Show comment
Hide comment
@mklement0

mklement0 Oct 8, 2018

My bad: I should have read the entire page at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings - the behavior is already documented - sorry for the noise.

mklement0 commented Oct 8, 2018

My bad: I should have read the entire page at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings - the behavior is already documented - sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment