I'm working with a recipe database application and I need the ability to convert natural language strings to numbers and vice versa. For example, I need 1 1/4 to convert to 1.25, and I'll nee开发者_StackOverflow中文版d to be able to convert 1.25 back into 1 1/4.
Is there any library or built-in functions that can do this?
Check this answer for representing mixed fractions and when you need to convert from the string back to mixed fraction split the string,convert the 3 string's to numbers and pass to the the MixedFraction
class.
None that I know of in the Java libraries, but there are some online.
Apache has a Fraction class. Here's it's API http://commons.apache.org/math/apidocs/org/apache/commons/math/fraction/Fraction.html
The constructor Fraction(double value)
, Fraction(int num, int den)
, and the methods add(Fraction fraction)
and doubleValue()
are what you want, I think.
精彩评论