I want to add a 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?Describe how I can fix this?
double a = 1.5; Interaction.MsgBox(string.Format("{0:#,###0.#}", a));
Here's how to do it:
string.Format("{0:0,0.0}", a)
He is doing it right. #, ## 0 means writing at least one digit and decimal places with zeros and spaces with comas. Therefore, it is rounded from 1.5 to 2, since it cannot write decimal numbers. Instead, try #, ## 0.00. You will receive 1.50
Try the following format:
string.Format("{0:#,0.0}", a)
There is a standard format string that will separate thousands of units: N
N
float value = 1234.512; value.ToString("N"); // 1,234.512 String.Format("N2", value); // 1,234.51
You tried: -
string.Format("{0:0,000.0}", 1.5);