Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am getting the error like input string was not in correct format error while converting string to Float or Decimal.
For example My value is 8.0.8 or
String s ="0.0.8";
how to convert the above value from string to float.
I have been read and tried so many ways but it's not working.
Could you please suggest any solution to me.

What I have tried:

I have tried below solutions but it's not working and tried other solutions also
var dd = Convert.ToDouble(_oTable.Rows[0]["Version"].ToString());
                    CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                    ci.NumberFormat.CurrencyDecimalSeparator = ".";
                  double   _nAIRVersion1 = double.Parse(_oTable.Rows[0]["Version"].ToString(), NumberStyles.Any, ci);
Posted
Updated 29-Nov-22 4:32am
Comments
Sandeep Mewara 29-Nov-22 10:33am    
What's the expected float value out of it? If you see there can be multiple ways to read and thus there is no standard converter for it. You would need to define one of your own based on some rule and then use it.

string=>"0.0.8"
float=>0.0, 0.8, 8.0 => so shared string is not a value 1-1 float convertible value.

1 solution

You can't convert "0.0.8" into a double or float because those number types only allow one decimal point. If you want to parse this as an actual version you can use the Version Class (System) | Microsoft Learn[^]

C#
string text = _oTable.Rows[0]["Version"].ToString();

if (Version.TryParse(text, out Version version))
{
  // Do something with the version
}
 
Share this answer
 

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