String to float conversion | comma | localization | Bidule


#1

Hello,

This may be a localization problem, but I wanted to share as I spent half a day figuring out what was happening…

I have float values stored in an xml element inside my plugin (this is for factory preset value). (So they are stored as string like “0.707”).

Now to retreive the value of each string attributes from my xml, I use getAttributeValue().getFloatValue(), which work fine in most host. I get my string i.e. “0.707” converted to a float 0.707

But in plogue bidule, it always convert the string to float 0, because the comma is not used the same way.
Bidule expect 0,707 and not 0.707

If I to do this : getAttributeValue(i).replaceCharacter(’.’,’,’).getFloatValue()
in order to ‘prepare’ the string data before the conversion to float, then it works in bidule, but now it broke all other hosts (they will convert to 0).

So my question is how to handle this automatically ?

Thanks !

Salvator


#2

how annoying! I guess the only way to do it would be to find out whether the host is bidule, and handle it as a special case…


#3

Ok, I ended by doing this :

String t =(“0.5”);
float x = 0;

if (t.getFloatValue()==0.5)
x = xml->getAttributeValue(1).getFloatValue(); //normal case

else
x = xml->getAttributeValue(1).replaceCharacter(’.’,’,’).getFloatValue(); //case like plogue bidule

Quite easy at the end. I just hope the time spent trying to figure out what was happening will also serve someone else.

Salvator


#4

Thinking about is, this is actually quite a nasty problem. I guess bidule must be mucking about with the c library’s locale setting, and that’s breaking atof, which in turn breaks String::getDoubleValue(), which is used all over the codebase.

Luckily, atof is only called in one place in juce, so I only need to fix one function to sort it out… but annoyingly, it looks like I’ll have to write my own atof for it, which is a bit fiddly.


#5

I did solve this issue by always calling this at the beginning of my applications:

This way, all floating point numbers are always written with decimal dot and not commas within my application, regardless of the locale set by the host application (I had this problem with ProTools, not with bidule, but I bet it is the same).


#6

Hello,

setlocale (LC_ALL, "English"); didn’t worked, but

setlocale (LC_ALL, "en_US"); made the trick !

Thanks for leading me the good way !

Salvator


#7

That’s not a very good solution, because it’ll probably break bidule’s own internal string conversions, and who’s to say whether bidule (or some other host) will repeatedly keep changing the locale…

But don’t worry, I’ve just checked in some new code with a locale-independent number parser! Have a go and see if it helps.


#8

Seems to work ! :slight_smile:

Many thanks, for the fast reaction.

Salvator