Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a String to Floating number?? 1

Status
Not open for further replies.

rcsen

Programmer
Jan 11, 2001
51
US
Hi,

I would like to convert a string variable which contains fraction to a numeric value,


say,

125 1/2 to 125.50

Could you please suggest a solution?

Thanks in Advance.


-RCSEN.

 
Assuming blank space is your delimiter,


$number = "125 1/2";
($pre,$post)=split (/ /,$number);
$result=$pre + eval ( $post );



HTH


regards
CM
 
Good answer!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
BTW, if you want to make sure the number has only two decimals, like your example, you can use sprintf:
Code:
$result = sprintf("%.2f", $result);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi latch and Tracy,

Thank you very much for the help.


-RCSEN.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top