lua-users home
lua-l archive

Re: How to extract a floating point number locale-independantly

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> $ lua -e 'os.setlocale("nb_NO.utf-8") print(tonumber("1.0"))'
> nil
> $ lua -e 'os.setlocale("nb_NO.utf-8") print(tonumber("1,0"))'
> 1,0
This is as expected, right? (BTW, I had to use no_NO.UTF-8 in Mac OS X.)
 
> However it *doesn't* affect lua parsing; which seems to be against the manual.
> 
> >From http://www.lua.org/manual/5.3/manual.html#pdf-tonumber
> > The conversion of strings can result in integers or floats, according to the lexical conventions
> > of Lua (see §3.1).
The text in §3.1 (for both Lua 5.2 and 5.3) is
 A numeric constant (or numeral) can be written with an optional
 fractional part and an optional decimal exponent ...
What may be missing is that there is a *decimal point* before 
the fractional part, and this does not depend in the locale.
> $ lua -e 'os.setlocale("nb_NO.utf-8") print(load([[return 1,0]])())'
This can never work because ',' is never part of a float in the lexer.
Bottom line: in the lexer, float constants can only use decimal points,
as in the C locale. Perhaps the manual entry for tonumber should mention
that tonumber respects the separator of the locale, as you suggest.

AltStyle によって変換されたページ (->オリジナル) /