java - Double convert to Fraction -
i want double convert value fraction (in java).
example: 0.33333 -> 1/3
i have many examples fraction found, not me. numbers between 0 1.
has perhaps have idea?
tahnks,
i guess want approximate fraction representation of double value (like example)
a simple way try denominators 1 one:
double doubleval = 0.33333; double negligibleratio = 0.01; for(int i=1;;i++){ double tem = doubleval/(1d/i); if(math.abs(tem-math.round(tem))<negligibleratio){ system.out.println(math.round(tem)+"/"+i); break; } }
this prints 1/3
. if set double doubleval = 0.6363636;
, prints 7/11
.
you can change negligibleratio
bigger value if want more succinct fraction result.
Comments
Post a Comment