[Gambas-user] Converting strings to numbers with localization issues
Benoît Minisini
gambas at ...1...
Mon Aug 23 12:19:19 CEST 2010
> 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?
--
Benoît Minisini
More information about the User
mailing list