如何将字符串转换为int和十进制 [英] how to convert string to int and decimal
本文介绍了如何将字符串转换为int和十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我正在尝试这样做:将文本框的值等于emp类属性类型int和十进制
txtDept.text = emp.DepID // 字符串= int
txtSalary.text = emp.salary // 字符串=十进制
解决方案
请参阅以下内容:
^ ]
字符串 a = " 字符串 a = " 字符串到 int
使用=
Convert.ToInt32(字符串)
字符串为十进制
Convert.ToDecimal(字符串);
使用具有所需格式的ToString
方法,如下所示:
txtDept.Text = emp.DepID.ToString();
txtSalary.Text = emp.salary.ToString(" );
问题尚不清楚.如果需要将数字的string
表示转换为其原始值,则有几种方法.一种选择是使用Convert.ToInt32, Convert.ToDecimal
等方法,因为即使输入值是null
,这些方法也不会throw exception
,如下所示:
emp.DepID = Convert.ToInt32(txtDept.Text);
emp.salary = Convert.ToDecimal(txtSalary.Text);
Hello
i am trying to do that : equal text box value into emp class property type int and decimal
txtDept.text = emp.DepID // string = int
txtSalary.text = emp.salary // string = decimal
so ho i can convert that?
解决方案 Refer this:
http://lokeshbasana.wordpress.com/2009/07/16/asp-net-convert-string-to-date-date-to-string-string-to-intstring-to-doublestring-to-decimalobject-to-intobject-tostringstring-to-byte-array/[^]
string a = "1234″;
int b = Convert.ToInt16(a);
string a = "1234.34″;
decimal b = decimal.Parse(a);
String to int
use=
Convert.ToInt32(string)
String to Decimal
Convert.ToDecimal(String);
Use the ToString
method with required format as shown below:
txtDept.Text = emp.DepID.ToString();
txtSalary.Text = emp.salary.ToString("N2");
The question was not clear. If it is required to convert the string
representation of the number to its native value then there are several approaches. One option is to use Convert.ToInt32, Convert.ToDecimal
etc. methods as these will not throw exception
even when the input value is null
as shown below:
emp.DepID=Convert.ToInt32(txtDept.Text);
emp.salary=Convert.ToDecimal(txtSalary.Text);
这篇关于如何将字符串转换为int和十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文