Add a comma to the digits every three digits using C #

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?

+8
c #
source share
6 answers
  double a = 1.5; Interaction.MsgBox(string.Format("{0:#,###0.#}", a)); 
+9
source share

Here's how to do it:

  string.Format("{0:0,0.0}", a) 
+3
source share

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

+2
source share

Try the following format:

 string.Format("{0:#,0.0}", a) 
+2
source share

There is a standard format string that will separate thousands of units: N

 float value = 1234.512; value.ToString("N"); // 1,234.512 String.Format("N2", value); // 1,234.51 
+2
source share

You tried: -

 string.Format("{0:0,000.0}", 1.5); 
+1
source share

All Articles