Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello
i have a textBox to enter integer value to it for example cost of money.
i want to seperate digits from right to left with camma(,).
for example:(12,250,000)
i use this code:
C#
textbox1.text=decimal.parse(textbox1.text).Tostring("#,#");

but i can not enter zero after the first camma.and it will show error(input string was not in a correct format)
please help
Posted

Well this is easy if you want currency format:
C#
textbox1.text=decimal.parse(textbox1.text).Tostring("C");


or without the currency sign:
C#
textbox1.text=decimal.parse(textbox1.text).ToString("0,0.0", CultureInfo.InvariantCulture);


A list of quick formats are shown in the documentation:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^]

or you can custom format the string:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^]
 
Share this answer
 
v4
Comments
FM7 19-Aug-12 23:32pm    
no work!
Kenneth Haugland 19-Aug-12 23:39pm    
It works on my computer......
Kenneth Haugland 19-Aug-12 23:42pm    
If you type this insted:
textbox1.text=decimal.parse(textbox1.text).ToString("0,0", CultureInfo.InvariantCulture);
It wont take the decimal "." into account, but you should have figured that out nw if you had read the documentation :)
FM7 20-Aug-12 7:20am    
CultureInfo.InvariantCulture is not show in my computer
Kenneth Haugland 20-Aug-12 7:46am    
Add using System.Globalization; at the top of the application.... Above any text in the code :)
try it..for your given input
C#
Regex.Match(textbox1.Text,@"^[0-9]\d{2},[0-9]\d{3},[0-9]\d{3}$");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900