Jump to content

decimal placement in string


soma56

Recommended Posts

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..

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.