按键事件上的千位分隔符 [英] thousand separator on keypress event
问题描述
我有一个表单来获取客户信息,我必须在文本框中有一个数字值。根据文化,我的问题是在按键上将其格式化为十进制。例如,当我在文本框中键入100025时,我希望它在我写作时格式化为1.000,25。
我该怎么做?
谢谢?
可能类似
const string numericFormat = ###,###,###,## #,###,###,###跨度>; // 使其足够长,以适应您需要的范围的最大和最小小数
< span class =code-comment> // 如果此字符串超过需要的长度,则不会造成伤害
// ...
decimal myValue = // ...
myTextBox.Text = myValue .ToString(numericFormat);
详情请见:
http://msdn.microsoft.com/en-us/library/system.decimal.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.decimal.aspxtarget =_ blanktitle =新窗口> ^ ],
http://msdn.microsoft.com/en-us/library/fzeeb5cd.asp x [ ^ ],
http://msdn.microsoft.com/en-us/library/ dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/ library / 0c899ak8.aspx [ ^ ]。
此格式已全球化。即使使用'',''格式符号,在格式化字符串中它也会显示为'',''或''。'',具体取决于当前的线程文化。
我感谢Matt的修复和有关全球化的有用说明,请参阅下面的评论。
-SA
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?
It could be something like
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
这篇关于按键事件上的千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!