• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Javascript help with decimal conversion


I am fairly new to javascript so please excuse this question if it is really basic. I am writing a javascript web application that among other things needs to be able to ounces and turn them into pounds. this is simple enough by divding by 16. the problem is that I need to be able to take the remainder and display it in a fraction with denominator of 16. so if it comes out to 6.75 lbs i need to be able to say that this is 6 12/16 lbs. is this possible?
anyone who has any suggestions for me I would appreciate the help?
 
Convert your dividend (6.75) into int. That'll give you the 6. Subtract that int from the dividend to get the decimal. (.75) Multiply that decimal by 16 - that's your numerator. Put an if/then in so you don't put 7 0/16 lbs..
 
this works great for this example because .75 * 16 = 12. but what if it was something like 2.43 lbs so then i would get .43 * 16 = 6.88?
 
You would show 2 43/100....

You can convert the number to a fraction that has a denominator of 100. Then you need to find the least common denominator.
 
use modulus.
I don't know how to do typecasting in javascript, but that's what the int() is supposed to mean.

pounds = int(ounces / 16);

fraction = ounces % 16;
 
Back
Top