0

Float is not accepting suffix f when converting from string to float using C#. I want to convert string to float & my string already has "F" suffix within it. but the float is not accepting suffix f when converting from string to float & throws an exception.

static void Main()
{
    string any_str = "123.45F";
    float f = float.Parse(any_str);       
    Console.WriteLine(f);
    Console.ReadLine();
}
TaW
  • 53,122
  • 8
  • 69
  • 111
Nitin Rastogi
  • 171
  • 1
  • 13

4 Answers4

4

Basically float.Parse can't do anything with that f suffix. float.Parse will just take a string representing a numerical value, any extra nonnumerical characters will throw an exception. The f suffix is just for use, by you, in your code itself. So as fahime and Norse said you'll need to get rid of that 'f' before using float.Parse.

TaW
  • 53,122
  • 8
  • 69
  • 111
1

I don't think float.Parse has any build in functionality for this, but you could create an extension method for your own convenience.

public static float ToFloat(this string value)
{
    return float.Parse(value.TrimEnd('f', 'F'));
}

Use as such:

any_str.ToFloat();

EDIT: Some may suggest you use Regex to clear out any letters or symbols, but it is not very performant; only use it when you know you will have symbols other than the f or F at the end.

Norse
  • 121
  • 10
1

Why we don´t need suffix after conversion but during declaration we need it?

By default and by C# specifications every number has a floating point will consider as a Double variable so you need to use an F suffix to create a literal for this type.

// 3.14 is considered a double variable by the compiler so it gives you an error
float myFloat = 3.14; 

You might ask why a double variable with value 3.14 cannot be assigned or cast into a float variable?

http://net-informations.com/q/faq/float.html

The Decimal, Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type.

Float - 32 bit (7 digits) Double - 64 bit (15-16 digits) The main difference is Floats and Doubles are binary floating point types and a Decimal will store the value as a floating decimal point type. So Decimals have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy. But in performance wise Decimals are slower than double and float types.

You can read this answer as well.

And about Parse method:

float.Parse only requires a valid string which means only a string includes [0-9], sign character and the decimal point ( vary by culture for eg [,/.]).

So you only need to get rid of any non-valid chars from your strings then parse it.

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
  • You suggested using regex while you downvoted my answer because of the same solution!! – fahime Jun 09 '19 at 06:17
  • @fahime I don't give him a solution, He already knows that he can remove the `f` at the end of it, he just wants to know why `f` is required during the declaration and etc ... – Ali Bahrami Jun 09 '19 at 06:19
-1

You need to remove all alphabetical chars in your input:

public static void Main()
{
    string any_str = "123.45F";
    string numberOnly = Regex.Replace(any_str, "[^0-9.]", "");
    float f = float.Parse(numberOnly);       
    Console.WriteLine(f);
    Console.ReadLine();
}
fahime
  • 179
  • 2
  • 12
  • He wants to see if `Parse` could parse it or not. – Ali Bahrami Jun 09 '19 at 05:43
  • He wants to convert a string to float and faced an exception! So it could be the solution. – fahime Jun 09 '19 at 05:47
  • 2
    This code with neither work for valid strings like `-1`, `1.23E+10`, `NaN`, `Infinity` nor for certain locales which use other characters than a period as a separator (e.g. in German it‘s 123,45). Instead of a whitelist approach, a blacklist approach like the one proposed by @Norse is better. – ckuri Jun 09 '19 at 06:03