lua-users home
lua-l archive

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


On Jul 26, 2011, at 11:29 AM, Petite Abeille wrote:

> FWIW, here is a little tobase function:
...

Thank you for the example.

Some people may prefer to change

>    local aNumber = abs( floor( tonumber( aNumber ) ) )

to 

>    local aNumber = floor ( abs( tonumber( aNumber ) ) )


for symmetry (as numbers are normally printed).

Original version:

> print( tobase( 6700417.01, 36 ), tonumber( tobase( 6700417.01, 36 ), 36 ) )
3ZM2P	6700417

> print( tobase( -6700417.01, 36 ), tonumber( tobase( -6700417.01, 36 ), 36 ) )
-3ZM2Q	1.8446744073703e+19

Revised version:

> print( tobase( 6700417.01, 36 ), tonumber( tobase( 6700417.01, 36 ), 36 ) )
3ZM2P	6700417

> print( tobase( -6700417.01, 36 ), tonumber( tobase( -6700417.01, 36 ), 36 ) )                                                                                                   -3ZM2P	1.8446744073703e+19

This also shows that tonumber (in Lua 5.1.4) has trouble with negative number strings not in base 10.

> return tonumber('-3ZM2P', 36)
1.8446744073703e+19

> return tonumber('-3456', 10)
-3456

> return tonumber('-1010', 2)
1.844674407371e+19

e