[英]Format a text box to display user input on runtime in form of thousand separator number with decimals
i am still new to c#, i am trying to build a simple calculator with c# windows form.我还是 c# 的新手,我正在尝试使用 c# windows 形式构建一个简单的计算器。 i did all the coding but i am stuck in formating my calculator screen(text box in this case) to display numbers with thousand separator comma and decimal point.
我做了所有的编码,但我被困在格式化我的计算器屏幕(在这种情况下为文本框)以显示带有千位分隔符逗号和小数点的数字。
for example: if the user input 1000 i want it to display 1,000 of if the user input 1000.0123 i want it to display 1,000.0123 on the fly(just like the windows 10 calculator)例如:如果用户输入 1000 我希望它显示 1,000 如果用户输入 1000.0123 我希望它即时显示 1,000.0123(就像 windows 10 计算器一样)
i really appreciate the any help, i have been searching all around the internet for some solution and came to nothing我真的很感谢任何帮助,我一直在互联网上搜索一些解决方案,但一无所获
use string format, in the textbox where you show the numbers:在显示数字的文本框中使用字符串格式:
txtNumber.text = string.Format("{0:n}",12521452.646521); // 12,521,452.64
or:或者:
txtNumber.text = string.Format("{0:#,0.#########}", 12521452.646521)) // 12,521,452.646521
see more: C# String Format Method查看更多: C# 字符串格式方法
var input = 1024.10;
Console.WriteLine(input.ToString("N"));
Output: Output:
1,024.10
1,024.10
Format as a numeric value with 2 decimal places.格式为带有 2 位小数的数值。 For 3 or 4 decimal places you can use ToString("N3") or N4.
对于 3 或 4 位小数,您可以使用 ToString("N3") 或 N4。 For No decimal places ToString("N0")
对于没有小数位 ToString("N0")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.