Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
I am trying to add commas to a number for the presentation layer and need to cast and then split the number on every third character in order to join on a ‘,’.
So if i have a string like this
546546555
desired output:
546,546,555
Other times, the number could be longer or shorter:
254654
desired output:
254,654
Is it possible to split in this manner then join with a comma?
tahnks!
EDIT:
Hi Everyone,
Thanks very much for your help.
To add to this post I also found a way to do this in SQL:
SUBSTRING(CONVERT(varchar, CAST(NumItems AS money), 1), 0, LEN(CONVERT(varchar, CAST(NumDocs AS money), 1)) – 2) as [NumDocs]
Rather than splitting the string manually, you should convert it to a number (or leave it as a number), and call the
ToStringmethod with the appropriate formatting:Example
See this MSDN page for different format values:
C – Currency format
D – Decimal format
E – Scientific format
F – Fixed point format
G – General format
N – Number format
P – Percent format
R – Round trip format
X – Hexadecimal format