String and Float

How can I make a string to a float in JUCE?

Hi,
You have : String.getFloatValue();

Ok,
Thank you:slightly_smiling_face:

And you can make sure it’s a string that contains numbers like that:

bool is_number(const String& str)
{
	return str.containsOnly("0123456789.-");
}

or you can implement something like this:

float stringToFloat (String& str)
{
    if(str.containsOnly("0123456789.-"))
    {
        return str.getFloatValue();
    }
    else
    {
        return 0.0f;
    }
}

Ok,
Thank you very much.

Default behaviour is to stop parsing, as soon as a character is hit, that is not parsable.
e.g. “1.5368.24” would stop parsing when the second . is hit. In a post 2 weeks ago I showed a method, how to verify, if the number is really fully parseable:

Oh yes you’re right, indeed if the string contains a double character it’s the bug!
If I test a string with “2.78.3”, the function returns 2.78, which is not so bad but is not fair…

Finally String.getFloatValue(); is not so badly used alone because if I test:
“2X78.3”.getFloatValue(); // it returns 2
“X278.3”.getFloatValue(); // it returns 0