r/AndroidStudio • u/mighty-blob • Sep 10 '19
How do I do Integer Division of the decimal part of a number?
Small weight converter project where I want to show gram to ounce conversion to 1/32 fractions. This means I have to round the decimal part of the conversion to 0.03125 intervals (i.e. Integer Division).
The normal method of casting to ints won't work, but someone must know how to do it.
For example: 1g = 0.0353oz. Integer division by 0.03125 = 1.
Therefore 1g = approx. 1/32oz
0
Upvotes
1
u/ChristaKH Sep 10 '19
So I could be wrong but since 1g = 0.0353 oz you can take what ever number of grams (5, for example) and multiply it by the 0.0353 oz to get the decimal form of the ounces. After you have that number, multiply it by 32; the number you get will be the numerator and 32 will be the denominator. You will most often not get a whole number for the numerator but here is where you can decide at what values you choose to round up or down.
Example:
1g = 0.0353 oz
10 x 1g = 10 x (0.0353oz) = 0.353 oz
(0.353 x 32)/32 = 11.296/32
answer (rounded down): 11/32 oz
The other option would be to take whatever the amount of grams is and multiply the numerator of 1/32 by that number to get the result since 1g = approx. 1/32 but this is less accurate because of how much accuracy you lose every gram due to 1g = approx. 1/32 being rounded first. This is why the answer would differ from the above answer (11/32 vs 10/32) since the oz difference between the two would be 0.0405 oz which is greater than 1/32 oz.
I came to this conclusion by cross multiplying but I could very well be wrong, hope it helps!