如何在C#中将字符串转换为float [英] How to convert string to float in C#
问题描述
如何将这些字符串str = 1.2222222222转换为浮动
我试过float.parse(str);
但我没有得到任何结果
我已经采取了存储过程和这样的代码
if (cn.State!= ConnectionState.Open)
cn.Open();
cmd = new SqlCommand( SP_Exchange_Rate_Update ,cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ SHORT_NAME,Txt_Currency.Text);
cmd.Parameters.AddWithValue( @ CURRENCY,Txt_Desc.Text);
cmd.Parameters.AddWithValue( @ EXCHANGE_RATE,Convert.ToDouble(Txt_Rate.Text ));
cmd.ExecuteNonQuery();
cn.Close();
loaddata();
LoadCurrency();
Txt_Currency.ReadOnly = true ;
@EXCHANGE RATE我已经漂浮了。
当我跑步时,我得到的结果是小数点两位,我得到了近似值。
例如Txt_Rate .Text = 1.225222222我已经给出了我得到的结果1.23
你可以解决这个错误
参见下面的代码
string str =1.225222222;
十进制值= 0;
decimal.TryParse(str,out value);
value = Math.Round(value,2);
你好,
试试这种方式
string str =1.225222222;
string value = Math.Round(Convert.ToDouble(str),2).ToString();
现在在您的代码中传递此值作为汇率
谢谢
string str = 3.14159;
float fnum = float .Parse(str);
How to convert these string str=1.2222222222 to float
I tried float.parse(str);
but i did n't get any result
I have taken Stored Procedure and written code like this
if (cn.State != ConnectionState.Open)
cn.Open();
cmd = new SqlCommand("SP_Exchange_Rate_Update", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SHORT_NAME", Txt_Currency.Text);
cmd.Parameters.AddWithValue("@CURRENCY", Txt_Desc.Text);
cmd.Parameters.AddWithValue("@EXCHANGE_RATE",Convert.ToDouble(Txt_Rate.Text));
cmd.ExecuteNonQuery();
cn.Close();
loaddata();
LoadCurrency();
Txt_Currency.ReadOnly = true;
At @EXCHANGE RATE I have taken float.
While I was running I got the result upto two decimal points I got approximate value.
For Example Txt_Rate.Text=1.225222222 I have given I got the result 1.23
Can u pls solve this error
See the following code
string str = "1.225222222"; decimal value = 0; decimal.TryParse(str, out value); value = Math.Round(value, 2);
Hello ,
Try this way
string str = "1.225222222"; string value= Math.Round(Convert.ToDouble(str), 2).ToString();
now pass this value in your code as exchange rate
thanks
string str = "3.14159"; float fnum = float.Parse(str);
这篇关于如何在C#中将字符串转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!