[Gambas-user] Converting strings to numbers with localization issues
Ron
ron at ...1740...
Mon Aug 23 14:55:16 CEST 2010
Fabien,
you are right, of course... they will be translated by Str$
I changed PRINT to WRITE just to test, and the pattern is the same for
all 4.
Finally a way to get this fixed, now I have to check my massive project
for my wrongly used Val()'s
Thanks both Benoît and Fabien.
Regards,
Ron_2nd.
> CFLoat return a Float variable ... so PRINT just see a variable and
> not a convertion funtion ... PRINT do what it do for every float,
> display the number in localization settings.
>
>
>
> 2010/8/23 Ron<ron at ...1740...>:
>> On 23-8-2010 12:19, Benoît Minisini wrote:
>>>> On 23-8-2010 11:54, Benoît Minisini wrote:
>>>>>> 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,
>>>> I have noticed that when using CFloat(sValue) I have to check/translate
>>>> commas to dots first if found in string, otherwise CFloat says 'wanted
>>>> float got string instead'
>>>>
>>>> Would be handy to know what Val() does behind the scene, do you know
>>>> without having to search through your code? It's hard for me to find.
>>>>
>>>> Thanks
>>>>
>>>> Regards,
>>>> Ron_2nd.
>>>>
>>> What Val() does is explained in its wiki documentation page. What information
>>> do you need that is not in that page?
>>>
>> I was thinking about the code behind it, but it's not important.
>>
>> One last question, is the behaviour below normal?
>> The first CFloat (en_US) returns a dot while the rest returns a comma in
>> the result, ie the are not consistant either, I must have overlooked
>> something..or .?
>>
>> PUBLIC SUB Main()
>>
>> DIM sValue AS String = "31,72"
>>
>> ' replace , by .
>> sValue = Replace(sValue, ",", ".", gb.String)
>>
>> PRINT "Original Value -> "& sValue
>>
>> PRINT System.Language& " -> ";
>> PRINT CFloat(sValue)
>>
>> System.Language = "nl_NL.UTF-8"
>> PRINT System.Language& " -> ";
>> PRINT CFloat(sValue)
>>
>> System.Language = "de_DE.UTF-8"
>> PRINT System.Language& " -> ";
>> PRINT CFloat(sValue)
>>
>> END
>>
>> OUTPUTS:
>>
>> Original Value -> 31.72
>> en_US.UTF-8 -> 31.72
>> nl_NL.UTF-8 -> 31,72
>> de_DE.UTF-8 -> 31,72
>>
>> Regards,
>> Ron_2nd.
>>
>>
> e.net/lists/listinfo/gambas-user
More information about the User
mailing list