vb.net VS2010:格式化货币文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6652907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VS2010 : Format a text box for currency
提问by jonny
I have check boxes selecting prices that go straight to the text box, how can I make it so it will display $ and two decimal places?
我有选择直接进入文本框的价格的复选框,我怎样才能让它显示 $ 和两位小数?
Code:
代码:
Dim total As Double
If rb_s1.Checked = True Then
txt_1.Text = "650.00"
Else
txt_1.Text = ""
txt_1.Text = total
回答by MGZero
use the formatcurrency() method.
使用 formatcurrency() 方法。
txt_1.text = formatcurrency(650.0)
EDIT: Please remember to use YOUR variable names and to not copy and paste sample code. This format will work with your code when placed into your if statement.
编辑:请记住使用您的变量名称,不要复制和粘贴示例代码。将此格式放入 if 语句时将适用于您的代码。
回答by 4o66
Numeric Datatypes have a ToString method you can call. ToString() will just convert the numeric value to a string, but you can optionally specify a format, by putting the format in as the method parameter.
数字数据类型有一个可以调用的 ToString 方法。ToString() 只会将数值转换为字符串,但您可以选择指定格式,方法是将格式作为方法参数。
I don't know all the formats, but I do know "C2" is currency with 2 decimal places. For example, in your posted code:
我不知道所有的格式,但我知道“C2”是带有 2 个小数位的货币。例如,在您发布的代码中:
Dim total As Double
If rb_s1.Checked = True Then
txt_1.Text = "650.00"
Else
txt_1.Text = String.Empty 'String.Empty is just a more precise way than ""
txt_1.Text = total.ToString("C2")
回答by SOPHORS MEN
I found solution how to convert to Currency I ok
我找到了如何转换为货币的解决方案我没问题
*
*
- dim test as string
- test="1000"
- txtBalance.Text = CDbl(result).ToString("#,##0.00")
- 将测试作为字符串变暗
- 测试=“1000”
- txtBalance.Text = CDbl(result).ToString("#,##0.00")
*
*
回答by Mohamed Ibrahim
I found solution how to convert to Currency. Try this :
我找到了如何转换为货币的解决方案。尝试这个 :
Textbox1.Text = String.Format("{0:n2} $", CType(Textbox1.Text, Double))