I want to add comma to decimal numbers every 3 digits using c#.
I wrote this code :
double a = 0;
a = 1.5;
Interaction.MsgBox(string.Format("{0:#,###0}", a));
But it returns 2.
Where am I wrong ?
Please describe how can I fix it ?
A comma is placed every third digit to the left of the decimal point and so is used in numbers with four or more digits. Continue to place a comma after every third digit. For example: $1,000,000 (one million dollars)
First comma is placed three digits from the right of the number to form thousands, second comma is placed next two digits from the right of the number, to mark lakhs and third comma is placed after another next two digits from the right to mark crore, in Indian system of numeration.
We use commas every three digits to the left of the decimal point because it's important to know how many digits there are. It's less important to know how many digits there are to the right of the decimal point. The number of digits to the left of the decimal point tells you how big the number is.
There is a standard format string that will separate thousand units: N
float value = 1234.512;
value.ToString("N"); // 1,234.512
String.Format("N2", value); // 1,234.51
Here is how to do it:
string.Format("{0:0,0.0}", a)
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