This code is from Charles Pettzold's "Programming Windows Sixth Edition" book:
public object Convert(object value, Type targetType, object parameter, string language) { return ((double)value).ToString("N0"); }
ToString("N0")
is supposed to print the value with comma separators and no decimal points. I cannot find the reference to appropriate ToString
overload and "N0"
format in the documentation. Please point me to the right place in .NET documentation.
For N0 the actual output will no contains digits after the decimal point (like in integer values). Align numbers with spaces.
string format - uses under the hood- StringBuilder - which is much faster for working with strings. ToString is the default representation of an object. Follow this answer to receive notifications.
Basically, ToString("N2") will use the CultureInfo to format the number. This means that your thousands separator might be different depending on the used CultureInfo . You can also pass the desired CultureInfo if you want.
The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier. value.ToString("C3", CultureInfo.CurrentCulture)
Checkout the following article
on MSDN about examples of the N
format. This is also covered in the Standard Numeric Format Strings
article.
Relevant excerpts:
// Formatting of 1054.32179: // N: 1,054.32 // N0: 1,054 // N1: 1,054.3 // N2: 1,054.32 // N3: 1,054.322
When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:
- On the .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).
- On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With