Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add commas to numbers! 1

Status
Not open for further replies.

autumnEND

Programmer
Nov 1, 2005
61
0
0
GB
Hi, is there a way to add commas to numbers. Maybe a regular expression. Which would make numbers easier to read.

for example:

111222333 to 111,222,333
12345 to 12,345
123 unchanged

Any help would be greatly appreciated .I cant seem to find a solution. Thanks in advance .
 
A couple options I can think of...

1. if you're outputting using .ToString() then you can use a formatting string.

2. if you are displaying a number using something like a datagrid then you will want to set the current culture info manually to a culture that includes the , by default.

If you use option #1 - I recommend writing a simple class that has a static tonumberstring method such as:

public static string tonumberstring(int num)


Enjoy googling - also check out the msdn.microsoft.com site for formatting strings.
 
Use the formatting string -- if you use one of the predefined numeric formatting strings, you'll get cultural independence as a side-benefit.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
autumnEND,

You could use a float and then use a mask to output it like:

float myAmount = 1000000;
myAmount.ToString("#,##0.00;#,##0.00;0");

Wux.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top