Not a bug XenForo_Input::FLOAT does not take locale into account

Jon W

Well-known member
In XenForo_Input, replacing:
PHP:
            case self::FLOAT:
                $data = floatval($data);
            break;

with something more like:
PHP:
            case self::FLOAT:
                $language = XenForo_Visitor::getInstance()->getLanguage();
                $data = str_replace($language['thousands_separator'], "", $data);
                $data = str_replace($language["decimal_point"], ".", $data);
                $data = floatval($data);
            break;
would allow users to enter decimals based on their selected locale.

Similarly in XenForo_DataWriter for TYPE_FLOAT.
 
I think it would confuse users. With the suggested code, 3.5 becomes 35 with German settings. A different number. ;) To solve this, the script must determine what the user input means. The script would have to to deal with a lot of variants of user input data. There is some example code on php.net.

Let it be as is and keep it simple. :)
 
I think it would confuse users. With the suggested code, 3.5 becomes 35 with German settings. A different number. ;) To solve this, the script must determine what the user input means. The script would have to to deal with a lot of variants of user input data. There is some example code on php.net.

Let it be as is and keep it simple. :)

Well, if it's set to the german language (and the language creator set the language options correctly), then it would end up correct.

It uses the users' selected language.

Liam
 
The datawriter version should definitely not be changed. That reflects internal type changes.

The input version can't really be changed either as this is a massive backwards compatibility break. Anywhere that uses the "standard" format of aaaa.bbb to represent a float would fail if "." represented a thousands operator. As an example, this is used for media cropping and tagging with values that are JS generated. I'm not positive whether browsers actually respect alternate formats in input[type=number].

So if this is something you'd want to support, I think you'll need to do it manually. That said, there's a reason that we don't use floats if we can avoid it. :)
 
Back
Top Bottom