Converting a float into a string fraction representation

后端 未结 6 897
一整个雨季
一整个雨季 2021-01-17 20:23

In Java, I am trying to find a way to convert a float number into a fraction string. For example:

float num = 1.33333;
String numStr = Convert(num); // Shoul         


        
6条回答
  •  青春惊慌失措
    2021-01-17 20:39

    This might be of help:

    http://www.merriampark.com/fractions.htm

    Otherwise you'd need some way of telling Convert() how far out you want to take things. Maybe a maximum reduced demoninator or something like that. That way you'll get "1 1/3" for both of the first two examples you have above rather than "1 33333/100000" for the first and "1 333/1000" for the second.

提交回复
热议问题