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.
This question is related to
c#
.net
number-formatting
This is where the documentation is:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9) ...
And this is where they talk about the default (2):
// Displays a negative value with the default number of decimal digits (2).
Int64 myInt = -1234;
Console.WriteLine( myInt.ToString( "N", nfi ) );
Here is a good start maybe
Have a look in the examples for a number of different formating options Double.ToString(string)
It is a sort of format specifier for formatting numeric results. There are additional specifiers on the link.
What N
does is that it separates numbers into thousand decimal places according to your CultureInfo and represents only 2 decimal digits in floating part as is N2
by rounding right-most digit if necessary.
N0
does not represent any decimal place but rounding is applied to it.
Let's exemplify.
using System;
using System.Globalization;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
double x = 567892.98789;
CultureInfo someCulture = new CultureInfo("da-DK", false);
// 10 means left-padded = right-alignment
Console.WriteLine(String.Format(someCulture, "{0:N} denmark", x));
Console.WriteLine("{0,10:N} us", x);
// watch out rounding 567,893
Console.WriteLine(String.Format(someCulture, "{0,10:N0}", x));
Console.WriteLine("{0,10:N0}", x);
Console.WriteLine(String.Format(someCulture, "{0,10:N5}", x));
Console.WriteLine("{0,10:N5}", x);
Console.ReadKey();
}
}
}
It yields,
567.892,99 denmark
567,892.99 us
567.893
567,893
567.892,98789
567,892.98789
You can find the list of formats here (in the Double.ToString()-MSDN-Article) as comments in the example section.
Source: Stackoverflow.com