I am creating an app that uses percentages. Multiple items are created by the user in order to make them add up to 100%. For example: if you have 3 items, each at 33.3%, this means that you are missing 0.1%. Fractions are a lot cleaner as they are the exact number. I was wondering whether I would be able to give the user a choice of either typing in a double or a fraction. Can anybody give me any assistance in how to tackle this? I have looked at inputTypes and things like that, but I just can't find anything on fractions.
Thanks in advance!
As @Bartek correctly pointed out, Android doesn't have native facilities to manipulate fractions. And it makes sense, because to represent a fraction you would need to have a new "object" that stores 2 values: numerator and denominator. Manipulating such objects will become increasingly hard, because each addition will require searching for the lowest common denominator. And we can't really settle on fractions. How do we accurately represent a square root of three? And then there's the next question, how do we calculate a sum of a square root of three and a cube root of eighteen and three quarters?
So I would suggest using doubles everywhere in calculations, but round them to a second digit after comma when printing it on the screen. I.e. when user types 100/3, keep 33.3333333
in memory, but print 33.33 on the screen. If you add three of those, you get 99.9999999 which when rounded to the second digit will show 100.00. I do realize that this approach can introduce certain error, but for actual real life applications this error might be negligible.