Customer Portal

Locale - European or UK number/date formats handling

Comments 1

  • Avatar
    David Pavlis
    0
    Comment actions Permalink

    Not sure what exactly is the problem. If just the parsing, then this "#,##0.###" should be the UK format, however, instead of passing in the format string, you can just pass empty string "" and locale. It should then take the default format/parse string for that locale.

    Also, keep in mind this (from Clover docs) in case you would have numbers with spaces (formatted):

     
    Space as group separator

    If locale with space as group separator is used, there should be a hard space (char 160) between digits to parse the number correctly.

    For formatting decimal value back to string, respecting particular locale, you can use function formatMessageWithLocale.

    formatMessageWithLocale("es.ES","{0}",123456.78D);
    formatMessageWithLocale("en.GB","{0,number,###,###.##}",123456.78D);

    Btw - if you are dealing with numbers/strings which may be in US or UK or other and need to convert them to decimal, you can also use conditional fail expression or try-catch block to simply attempt different conversions and just go with the one which does not fail - as checking for some specific characters to determine whether you should use US or UK or other is problematic.

    decimal mydecimal;

    //conditional fail expression
    //if first fails, then second is attempted, then third,etc.

    mydecimal = str2decimal(input,"#,##0.###", "en.GB") : str2decimal(input,"##.##","cs.CZ");

    //the same effect, but using try-catch with potentially better control
    //over what happens if expression fails
    boolean success=false;
    try{
    mydecimal = str2decimal(input,"#,##0.###", "en.GB");
    success=true;
    }catch(CTLException ex){
    }
    if (!success){
    try{
    mydecimal = str2decimal(input,"##.##","cs.CZ");
    success=true;
    }catch(CTLException ex){
    }
    }

     

Please sign in to leave a comment.