Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
hi,

i have a form to get customer informations and i must have a number value in a textbox. my problem is getting it formatted as decimal on key press according to culture. forexample when i typed 100025 into a textbox i want it to format as 1.000,25 while i am writing.

how can i do this?

thank you?
Posted
Comments
Sergey Alexandrovich Kryukov 11-Apr-13 15:45pm    
Did you simply try to read documentation?
—SA

1 solution

It could be something like
C#
const string numericFormat = "###,###,###,###,###,###,###"; // make it long enough to fit maximum and minimum decimal of the range you need
// if this string is longer then needed, it won't harm

//...

decimal myValue = //...
myTextBox.Text = myValue.ToString(numericFormat);


For further detail, please see:
http://msdn.microsoft.com/en-us/library/system.decimal.aspx[^],
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

[EDIT]

This format is already globalized. Even though the format notation of ',' is used, in formatted string it will appear as ',' or '.', depending on current thread culture.

I appreciate Matt for a fix and a useful note on globalization, please see the comments below.


—SA
 
Share this answer
 
v7
Comments
Sergey Alexandrovich Kryukov 11-Apr-13 15:49pm    
Matt, thank you very much for the fix. I removed comments to avoid confusion...
—SA
CPallini 11-Apr-13 15:50pm    
5.
Sergey Alexandrovich Kryukov 11-Apr-13 15:55pm    
Thank you, Carlo.
—SA
Maciej Los 11-Apr-13 16:11pm    
Now (after EDIT) my 5!
I'm happy that my answer point you to add information about globalization ;)

One more time, thank you, Sergey!
Sergey Alexandrovich Kryukov 11-Apr-13 17:19pm    
Thank you, Maciej.
—SA

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