Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
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

C#
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
Posted
Updated 19-Sep-14 0:12am
v2

See the following code
string str = "1.225222222";
decimal value = 0;
decimal.TryParse(str, out value);
value = Math.Round(value, 2);
 
Share this answer
 
v2
Comments
TarunKumarSusarapu 19-Sep-14 5:36am    
I want to write this in windows not console
[no name] 19-Sep-14 5:40am    
Simple so don't write the last line use direct value :)
TarunKumarSusarapu 19-Sep-14 6:04am    
I tried but i didn't get that accurate result
[no name] 19-Sep-14 6:05am    
what accurate and which is missing can u show us
TarunKumarSusarapu 19-Sep-14 6:16am    
I have Improved My Question did u see that
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
 
Share this answer
 
Comments
TarunKumarSusarapu 19-Sep-14 6:37am    
I tried but I have taken Exchangerate datatype as float.

When I pass value in the front end 1.25555555

then it takes same value at the backend 1.26
Animesh Datta 19-Sep-14 6:44am    
then what is the issue now ?and what you exactly want from the above?
TarunKumarSusarapu 19-Sep-14 7:06am    
yes I want accurate value
TarunKumarSusarapu 19-Sep-14 7:32am    
K I got it I used Convert.Todouble() method Thanks For ur valuable suggestions
Animesh Datta 19-Sep-14 7:33am    
glad to help you . please accept the solution if it works
C#
string str = "3.14159";
float fnum = float.Parse(str);
 
Share this answer
 
Comments
Hussainp 19-Sep-14 11:59am    
Yes...yes,.it is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900