1

I have a javascript function that converts feet into a usable object that will take the total feet and break it down into feet / inch / fraction.

In the example below the decimal fraction portion should be zero and my inch should be 4

Lets say I've got the following.

var inches = 40;

Now when I call the function below like so unPackDecimalFeet(40 /12);

  unPackDecimalFeet: function (feetInchFraction) {

        var inchFraction = (feetInchFraction - ~~feetInchFraction) * 12;
        return {
            feet: ~~feetInchFraction,
            inch: ~~inchFraction,
            fraction: inchFraction - ~~inchFraction
        };
    }

My return value is below.

feet: 3
fraction: 0.9999999999999964
inch: 3

**The above return value should read.

feet: 3
fraction: 0
inch: 4

How would I need to process the feet so that I can get the correct return value?

3 Answers 3

1

I'd say multiply it by 1000 in the beginning, then do your calculations, and then divide by 1000. IEEE Floating Point arithmetic is the problem here. If you need more calculations, look into BigInteger libraries. (BigInt, sometimes)

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please give me a break down.
0
var inchesIn;
inchesIn = 40;
document.writeln('Feet: ' + Math.floor(inchesIn/12));
document.writeln('Inches: ' + inchesIn%12);

Comments

0

You're doing a lot to try and save yourself from tiny little inconsequential fraction issues throughout your code. I think maybe simpler is better:

unPackDecimalFeet : (function (feet) {
    var flooredFeet = 0 | feet,
        flooredInches = 0 | ((feet - flooredFeet) * 12),
        inchFraction = (feet * 12) - (0 | (feet * 12));

    return {
        feet: flooredFeet,
        inch: flooredInches,
        fraction: +(inchFraction.toFixed(2)) //Rounded to two decimals
    };
})

The only "trickiness" there is with 0 | as an alternative to Math.floor(). Feel free to use Math.floor() if it makes you feel better about maintainability. Also, I rounded off the "fraction" part of the response to the nearest 100th of an inch. You can round more precisely if you want, but some rounding is encouraged to prevent things like 0.9999999999999994 or whatever due to IEEE-754 gone wild.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.