Malfist
Malfist

Reputation: 31795

Converting strings to numbers and back to natural language?

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 need to be able to convert 1.25 back into 1 1/4.

Is there any library or built-in functions that can do this?

Upvotes: 1

Views: 711

Answers (2)

Jake Stevens-Haas
Jake Stevens-Haas

Reputation: 1686

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.

Upvotes: 1

Emil
Emil

Reputation: 13789

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.

Upvotes: 2

Related Questions