Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add comma to numbers every three digits using C#

Tags:

c#

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 ?

like image 916
Shahin Avatar asked May 28 '11 11:05

Shahin


People also ask

How do you put a comma after 3 digits?

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)

How do you put commas between numbers?

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.

Why does every three digit number need a comma?

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.


2 Answers

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
like image 66
Richard Szalay Avatar answered Sep 26 '22 00:09

Richard Szalay


Here is how to do it:

 string.Format("{0:0,0.0}", a)
like image 34
Hogan Avatar answered Sep 22 '22 00:09

Hogan