1
votes

I have lua program, that loads data from string. I need to convert those data to double, using tonumber. Problem is, that under cygwin in Windows 7, I got nil, if number contains . insetad of ,. How to solve this ?

n = tonumber("7.5") -> gives nil
n = tonumber("7,5") -> gives 7.5
1
s = "7.5"; n = tonumber((s:gsub("[.,]",tostring(1.5):sub(2,2)))) - Egor Skriptunoff
@EgorSkriptunoff Is this a comment or an answer? - Oliver
@Schollii - This is a solution, I think. But I have not tested it. - Egor Skriptunoff
You have to know several things about every string and text file; The binary representation is never enough. Short list: Character set, encoding, locale. If you know the locale then you use lhf's answer. - Tom Blodget

1 Answers

1
votes

Try this:

old = os.setlocale("C")
n = tonumber("7.5")
os.setlocale(old)