parse string with inches expression

F

Frank Langelage

Does anybody know a free available class / lib to parse a dimension
expressed with inches and fractions to a float value?
Input e.g. String "24 1/16" -> Output Float 24.0625.
 
B

Brandon McCombs

Frank said:
Does anybody know a free available class / lib to parse a dimension
expressed with inches and fractions to a float value?
Input e.g. String "24 1/16" -> Output Float 24.0625.

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;
 
F

Frank Langelage

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] );
}

public double toDouble()
{
double temp = whole + ( numerator / denominator );
return temp;
}

public static void main( String[] args )
{
Fractions f = new Fractions( "24 1/16" );
System.out.println( f.toDouble() );

f = new Fractions( "1/4" );
System.out.println( f.toDouble() );

f = new Fractions( "1" );
System.out.println( f.toDouble() );

f = new Fractions( "5 1/4" );
System.out.println( f.toDouble() );
}
}
 
J

Joshua Cranmer

Frank said:
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] );
}

I prefer simpler regex:
Pattern p = Pattern.compile("(\\d*)\\s*(\\d+)/(\\d+)");
Matcher m = p.match(s);
if (m.matches()) {
whole = Integer.parseInt(m.group(1));
numerator = Integer.parseInt(m.group(2));
denominator = Integer.parseInt(m.group(3));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top