Search code examples
javafractionsoctal

How to convert Octal Fraction to Decimal?


For examle 10.5 should return 8.625 in Decimal. I've found examples on converting decimal fraction to octal, but not vise versa.

Use of inbuilt functions are okay.

These should help:


Solution

  • What you do is

    • check for - at the start and remember this for later.
    • convert the number into a long as if the . wasn't there.
    • remember where the . was and at the end divide by 8^(n) where n is the number of decimal places. You can do this with >> instead. Or even better use Math.scaleb(num, scale)
    • if the number was signed, add the sign back in.