soma56 Posted June 20, 2012 Share Posted June 20, 2012 I'm trying to figure out how to place a decimal in the first price of the following: From: $299 lb / 6.59/kg To: $2.99 lb / 6.59/kg While I was able to figure this out where the string was simply a numeric value.. // get last two digits of number $l2d = substr($price, -2, 2); // place decimal before the last two digits of the number $price = substr_replace($price, '.', -2, 2) . $l2d; I'm baffled at where to start with this.. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 divide by 100. Quote Link to comment Share on other sites More sharing options...
soma56 Posted June 20, 2012 Author Share Posted June 20, 2012 How do you divide $299 lb / 6.59/kg by 100? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2012 Share Posted June 20, 2012 Where is the data coming from, and what format is it stored in? Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 Well, first you have to extract the value. <?php $string = '$299 lb / 6.59/kg'; // find out where ' lb' is $end = strpos($string, ' lb'); // extract the numbers. we use $end-1 because in this case: // 4 is the offset of ' lb'. since we start at offset 1 instead // of offset 0, we will grab an extra character (4, when we only // want 3) $price = substr($string, 1, $end-1); echo $price; // Outputs 299 ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.