rewrite toDecimal without using ^ or reverse