[Gambas-user] Converting strings to numbers with localization issues

Benoît Minisini gambas at ...1...
Mon Aug 23 11:54:15 CEST 2010


>   In my project I have several temp values in the form of 31.72 C
> 
> They are stored as string and I convert them with Val(sValue)
> 
> My project is translatable, default language is en_US.UTF-8
> 
> If I change System.Language, Val doesn't know how to convert the value
> correctly (or it does it correctly, but I have to change my code)
> 
> Anyone had this problem and knows how to prevent the system from leaving
> out point or comma's or the complete value?
> 
> Example project with output:
> 
> ' Gambas module file
> 
> PUBLIC SUB Main()
> 
>    DIM sValue AS String = "31,72"
> 
>    PRINT "Original Value -> " & sValue
> 
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
>    System.Language = "nl_NL.UTF-8"
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
>    System.Language = "de_DE.UTF-8"
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
>    sValue = "31.72"
>    PRINT "\nOriginal Value -> " & sValue
> 
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
>    System.Language = "nl_NL.UTF-8"
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
>    System.Language = "de_DE.UTF-8"
>    PRINT System.Language & " -> ";
>    PRINT Val(sValue)
> 
> END
> 
> OUTPUT IS:
> 
> Original Value -> 31,72
> en_US.UTF-8 -> 3172
> nl_NL.UTF-8 -> 31,72
> de_DE.UTF-8 -> 31,72
> 
> Original Value -> 31.72
> de_DE.UTF-8 -> 3172
> nl_NL.UTF-8 ->
> de_DE.UTF-8 -> 3172
> 
> So if I store it with . or , it's false interpreted by at least one of
> the languages...
> I do not have the same values, I also store energy usage and other
> values in the form of 41.322 Watts, so the decimal point is not always at 2
> 
> I remember this type of discussion regarding currency, how was it solved?
> 
> Please any input is welcome.
> 
> Gambas2, gb.qt, Ubuntu 10.04
> 
> Regards,
> Ron_2nd.
> 

The meaning of "." and "," in numbers changes with the language (the 
"localization"), and Val() follows the localization settings, so all that is 
logical.

So use CFloat() instead of Val() to have a behaviour that does not depends on 
localization, and create your own conversion function with it.

Regards,

-- 
Benoît Minisini




More information about the User mailing list