posted 21 years ago
Jennifer, it sounds like you want to use powers of two in your denominator, rather than powers of 10. You can do that. Here's how:
Choose the largest power of two you want for your denominator. Let's say it's 64, for the sake of discussion; any will do.First, separate the integer part of the decimal from the fractional. It sounds like you've already done that.Next, multiply the fractional part of the decimal by 64, and round to the nearest integer (using Math.round()). That'll give you the numerator of the fraction.If you want to, reduce the fraction using the code you've already written.
This method won't give you an exact representation, as Joel pointed out, but it'll be close enough for your need, because you chose the denominator. (If you want less error, choose a bigger denominator.)