Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Changing the decimal separator to a coma globaly Symfony


I have a fairly large number of forms (over 40) in my project with a lot of floating number fields.

I'm looking for a nice way to change the decimal separator from "." to "," globally for the whole project - so my users can submit floats in localised format as "43,27" not "43.27".

As an addition also a fast way to format all floating point numbers displayed by $obj->getXXX() would be helpful, retaining the ability to perform arithmetical operations on them.

Answers (4)


Gabriel Pillet answers:

You could try using setlocale() globally (in your forms or in your controller), for example : setlocale(LC_NUMERIC, 'fr_FR'); should set the comma as default number separator.

Krzysztof Karolczak comments:

Ok, this might be it. There are three obstacles I guess:

1) setlocale() is platform dependent, ie.
Windows: SetLocale(LC_ALL, "Polish_Poland");
*nix: SetLocale(LC_ALL, "pl_PL");
2) Overriding the sfValidatorNumber to replace "," back to "." before doClean().
3) Fixing all java scripts that do maths in my project.

But I got it working. Any follow-ups, sugestions?


burgua answers: ?

Krzysztof Karolczak comments:

Setting sfNumberFormatInfo::getInstance()->setDecimalSeparator(',') doesn't change anything in the forms.

Krzysztof Karolczak comments:

Anyone knows what uses sfNumberFormat to format numbers? Is it used by forms in any way? It might be a good start...

burgua comments:

<strong>sfValidatorI18nFloat</strong> can be user expect as sfValidatorBase in your models.
Looks like working approach.


Bill Hunt answers:

Why not just override the getXXX() method in the model? If you use the parent::getXXX(), and wrap the result with number_format() it should work as expected.

Krzysztof Karolczak comments:

I would have to overwrite over 200 get/set functions and still doesn't help with forms, because they don't use those model getters and setters.


Nate Flink answers:

You could handle this by adding a little logic in the form classes and/or validators, in addition to the getters and setters.

Something like

$value = preg_replace("/(\d+),(\d\d)/", "$1.$2", $formattedValue);

Only way I can think of doing that globally for every form would be to override sfForm, which would be a mega hack -- unless your inheriting from a custom parent form class.

Another idea is it might be possible to write a filter to set stuff directly in the request/response objects.

I like the other guy's post about using the locale function, although I don't know if that will do the trick in every instance. I've also had trouble with certain aspects of the symfony l10 stuff.

Krzysztof Karolczak comments:

Any smart place to put that? I don't want to do it for every model / form separately.