A Ruby number converter adapted from my C code