Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String Fraction to Number

Reply
Thread Tools

String Fraction to Number

 
 
Peter
Guest
Posts: n/a
 
      05-06-2004
Does anyone have an example of how to convert a String "26 1/2" to double 26.5 ?

Peter

Thank You
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      05-06-2004

"Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Does anyone have an example of how to convert a String "26 1/2" to double

26.5 ?

double myDouble;
if(myString.equals("26 1/2")) myDouble = 26.5;

--
Mike W


 
Reply With Quote
 
 
 
 
Rob Shepherd
Guest
Posts: n/a
 
      05-06-2004
Peter wrote:
> Does anyone have an example of how to convert a String "26 1/2" to double 26.5 ?
>
> Peter
>
> Thank You



From the archives I dug out this method.
Last time i used it it worked (for my input data)
....
public static double fracNumericStringToDouble(String str)
{
double mydouble;

int firstpartindex = str.indexOf(" ");
String wholepart = str.substring(0, firstpartindex);

System.out.println(wholepart);

int secondpartindex = str.indexOf("/");
String numerator = str.substring(firstpartindex+1, secondpartindex);
System.out.println(numerator);

String denominator = str.substring(secondpartindex+1, str.length());
System.out.println(denominator);

mydouble = (Double.parseDouble(numerator) / Double.parseDouble(denominator));
mydouble += Double.parseDouble(wholepart);

return mydouble;
}
....

Please test

HTH

Rob
 
Reply With Quote
 
antroy
Guest
Posts: n/a
 
      05-06-2004
Should perhaps alter it as follows to take into account whole numbers:

Rob Shepherd wrote:
....
> public static double fracNumericStringToDouble(String str)
> {
> double mydouble;
>
> int firstpartindex = str.indexOf(" ");


if (firstpartindex == -1) firstpartindex = str.length();

> String wholepart = str.substring(0, firstpartindex);
>
> System.out.println(wholepart);
> int secondpartindex = str.indexOf("/");


String numerator = "1";
String denominator = "1";

if (secondpartindex != -1){

numerator = str.substring(firstpartindex+1,
> secondpartindex);
> System.out.println(numerator);
>

denominator = str.substring(secondpartindex+1,
> str.length());
> System.out.println(denominator);
>


}

> mydouble = (Double.parseDouble(numerator) /
> Double.parseDouble(denominator));
> mydouble += Double.parseDouble(wholepart);
>
> return mydouble;
> }
> ...
>
> Please test


Again not tested, and could of course be cleaned up (the String
numerator = "1"; is a bit of a hack for example).


--
Ant...
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      05-06-2004
"VisionSet" <(E-Mail Removed)> wrote in message news:<i1smc.59$(E-Mail Removed)>...
> "Peter" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > Does anyone have an example of how to convert a String "26 1/2" to double

> 26.5 ?
>
> double myDouble;
> if(myString.equals("26 1/2")) myDouble = 26.5;


Thanks for your help, but that's not exactly what I am looking for.
The String number can be any number with a fraction not just "26 1/2".
I am looking for a conversion function.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-06-2004
On 6 May 2004 06:50:08 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Peter) wrote or quoted
:

>Does anyone have an example of how to convert a String "26 1/2" to double 26.5 ?


parse the thrtee pieces, convert to double and compute value =
integral + numerator / demoninatator

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Mason Bryant
Guest
Posts: n/a
 
      05-06-2004
"VisionSet" <(E-Mail Removed)> wrote in message news:<i1smc.59$(E-Mail Removed)>...
> "Peter" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > Does anyone have an example of how to convert a String "26 1/2" to double

> 26.5 ?
>
> double myDouble;
> if(myString.equals("26 1/2")) myDouble = 26.5;


Cute. You might also try the following (hackish) solution.

String l_input = "26 1/2";
String[] l_parts = l_input.split("(\\s)+");
double l_value = 0;
for(int i = 0; i < l_parts.length; i++) {

String l_fractionParts[] = l_parts[i].split("(\\D)+");
if(1 == l_fractionParts.length) {
l_value += Double.parseDouble(l_fractionParts[0]);
} else if(2 == l_fractionParts.length) {
l_value += Double.parseDouble(l_fractionParts[0]) /
Double.parseDouble(l_fractionParts[1]);
}
}
System.out.println(l_value);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the fastest way of getting the fraction part of a floating point number? ME C++ 1 02-02-2006 09:05 PM
The Power of the Pixel; 5 Years of Advertising at Only a Fraction of the Normal Cost. walter.alex@gmail.com HTML 4 11-20-2005 11:18 PM
How to MULTIPLY by fraction ?? (making variable iir) Jam VHDL 2 09-15-2004 11:44 AM
Can I round or set the number of decimals or even make this a fraction? Schklerg Javascript 7 11-30-2003 03:46 AM
Re: fraction reducing funtion Sam Holden C++ 2 08-04-2003 04:45 AM



Advertisments