2

I want to show fractions instead of decimal value in a textView(answerview)

For example - 1.0625 (This value is an answer from some calculation)

I want to show 1.0625 in fractions (Added a sample image [Required fraction image]) enter image description here like 1(1/16) in textView.

This is not a constant value, the value keeps changing according to user calculations, so for every answer I want to convert into fractions and display

Sujay

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
  • To turn a `double` into a fraction, try http://stackoverflow.com/a/31586500/984823 - As you know doubles are inprecise from the start, so an approximation must be sought, do not expect to be able to reduce `10625/10000` (by gcd). – Joop Eggen Aug 29 '16 at 07:18

1 Answers1

4

Use kexanie to do that

In your case you should set text in your MathView [read kexanie README.md]

String text = "$$" + var1 + "\\frac{" + var2 + "}{" + var3 + "}$$";

enter image description here

Here var1 = 1, var2 = 1 and var3 = 16

Add a MathView in your xml

<io.github.kexanie.library.MathView
    android:id="@+id/formula"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    auto:engine="KaTeX"
    >
</io.github.kexanie.library.MathView>

Then in onCreate() method

MathView mathv = (MathView) findViewById(R.id.formula);

and finally set your formula to mathv

mathv.setText(text);

Or you can use other libraries mentioned here

Community
  • 1
  • 1
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24