String and Float



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


You have : String.getFloatValue();


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)
        return str.getFloatValue();
        return 0.0f;


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