Brandon said:
Uh, you can't create one yourself? That isn't a hard problem to solve
if you enforce certain formats for entering the information. You don't
even need a whole library in my opinion.
"24 1/16"
split using a space
"24" and "1/16"
float l = Float.parseFloat("24")
"1/16"
split using /
"1" and "16"
float n = Float.parseFloat("1")
float d = Float.parseFloat("16")
float r = n/d;
float f = l + r;
I thought this should be a common problem for programmers in North
America and I don't have ro reinvent a solution.
It's not that simple if you want get strings like "1/4" or "5 1/4" or "1".
I got another reply to my question from Dinesh Kapoor. I enhanced his
example to work with this different input:
public class Fractions
{
private double whole;
private double numerator;
private double denominator;
public Fractions( String s )
{
String[] result = s.trim().split( "\\s+" );
String wholePart;
String fractionPart[];
if ( result[0].matches( "\\d+" ) ) {
whole = Double.parseDouble( result[0] );
if ( result.length > 1 ) {
fractionPart = result[1].split( "/" );
} else {
fractionPart = new String[] { "0", "1" };
}
} else {
whole = 0;
fractionPart = result[0].split( "/" );
}
numerator = Double.parseDouble( fractionPart[0] );
denominator = Double.parseDouble( fractionPart[1] );
}