In Java, assuming I have a double
variable called decimal
with the assigned value of 0.333
, how would I display this as a "proper", formatted fraction - with the numerator over the denominator separated with a straight line (as opposed to simply displaying 1/3
)?
Edit: One of the solutions is to use Apache Commons Mathematics Library, namely the Fractions package. Referencing the doc:
A fraction number, can be built from two integer arguments representing numerator and denominator or from a double which will be approximated:
Fraction f = new Fraction(1, 3); // 1 / 3
Fraction g = new Fraction(0.25); // 1 / 4
So all you need to do after that is to display it, a simple way of doing that is using android.text.Html method fromHtml(String source, int flags) with superscript and subscript tags. Note that this was added in API level 24 and changed fromHtml(String source).