logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$4
Changing the decimal separator to a coma globaly

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.

This question has been answered.

Krzysztof Karolczak | 09/29/10 at 12:59pm Edit


(10) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    09/29/10
    1:15pm
    burgua says:

    http://www.symfony-project.org/api/1_4/sfNumberFormatInfo#method_setdecimalseparator ?

  • avatar
    Last edited:
    09/29/10
    1:52pm
    Bill Hunt says:

    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.

  • avatar
    Last edited:
    09/29/10
    3:23pm
    Nate Flink says:

    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.

    Previous versions of this answer: 09/29/10 at 3:23pm

  • avatar
    Last edited:
    10/03/10
    2:02am
    Gabriel Pillet says:

    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.

    http://php.net/manual/fr/function.setlocale.php

  • avatar
    Last edited:
    09/29/10
    1:41pm
    Krzysztof Karolczak says:

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

  • avatar
    Last edited:
    09/29/10
    2:34pm
    Krzysztof Karolczak says:

    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.

  • avatar
    Last edited:
    09/29/10
    2:37pm
    Krzysztof Karolczak says:

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

  • avatar
    Last edited:
    09/29/10
    2:39pm
    Krzysztof Karolczak says:

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

  • avatar
    Last edited:
    09/29/10
    3:41pm
    burgua says:

    http://symfonyguide.wordpress.com/2009/09/16/sfvalidatori18nfloat-a-homemade-validator-for-internationalized-input-numbers/

    sfValidatorI18nFloat can be user expect as sfValidatorBase in your models.
    Looks like working approach.

  • avatar
    Last edited:
    10/02/10
    6:01am
    Krzysztof Karolczak says:

    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?

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.