Using String.Format how can i ensure all numbers have commas after every 3 digits eg 23000 = "23,000" and that 0 returns "0".
String.Format("{0:n}", 0); //gives 0.00 which i dont want. I dont want any decimal places, all numbers will be integers.
For format String "%,. 2f" means separate digit groups with commas and ".
In Python, to format a number with commas we will use “{:,}” along with the format() function and it will add a comma to every thousand places starting from left. After writing the above code (python format number with commas), Ones you will print “numbers” then the output will appear as a “ 5,000,000”. Here, {:,}.
You can use java. util. text. NumberFormat class and its method setGroupingUsed(true) and setGroupingSize(3) to group numbers and add a comma between them.
You can do this, which I find a bit cleaner to read the intent of:
String.Format("{0:#,###0}", 0);
Example:
string.Format("{0:#,###0}", 123456789); // 123,456,789 string.Format("{0:#,###0}", 0); // 0
If your current culture seting uses commas as thousands separator, you can just format it as a number with zero decimals:
String.Format("{0:N0}", 0)
Or:
0.ToString("N0")
from msdn
double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
Displays 1,234,567,890
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