Slavisa Perisic
Slavisa Perisic

Reputation: 1110

Calculating total price from string value in php

I have a e-commerce shop and on the shopping cart page it gives me a separate price for every product, but I need total price.

in order to do that, I need to calculate all these values together and that's fine.

But, what bugs me is that I should calculate the sum of variables that are given in this format:

$455.00

What is the best way to extract the value "455" so I could add it to another value afterwards?

I hope I made myself clear...

Upvotes: 1

Views: 3408

Answers (7)

hakre
hakre

Reputation: 198117

Don't use float, but instead use an integer in cent. Floats are not precise (see Floating Point Precision), so the calculation tend to fail if you use floats. That's especially a burden if it is related to payments.

$str = '$455.00';
$r = sscanf($str, '$%d.%d', $dollar, $cent);
if ($r <> 2 or $cent > 99 or $cent < 0 or $dollar > 9999 or $dollar < 0) throw new Exception(sprintf('Invalid string "%s"', $str));

$amountInDollarCents = $dollar * 100 + $cent;

echo $str, ' -> ', $amountInDollarCents;

Demo

Upvotes: 4

Kjartan
Kjartan

Reputation: 19131

It's been a long time since I worked with PHP, so I don't know what the best practice would be for working with currency. One quick method would be to remove "$" and ".", and just add together the resulting as integers.

use str_replace() for instance, and replace "$" and "." with an empty string: https://www.php.net/manual/en/function.str-replace.php

This will give you the whole sum in cents (thus avoiding some potential rounding problems). You can then divide it by 100 and format it however you like to display the sum as dollars.

Upvotes: 0

Dmitri Snytkine
Dmitri Snytkine

Reputation: 1126

It really looks like your program is doing it wrong. You should really represent all prices as (double) instead of a string. Then only when you need to show the price to the user you would prepend the $ sign to it, converting it to a string. But your program should really treat prices as numbers and not strings. If you storing your price in the database as a string "$5.99" then you are really doing it wrong.

Upvotes: 0

stivlo
stivlo

Reputation: 85516

What about the following:

$amount = array();
$amount[0] = '$455.15';
$amount[2] = '$85.75';

$total = 0;
foreach ($amount AS $value) {
    $value = str_replace('$', '', $value);
    $total += $value;
}
echo $total . "\n";

The cleaning operation is:

$value = str_replace('$', '', $value);

You might want to extract it in a function, especially if you need to use it in more than one place.

Another thing to think about is, why do you have the value in such way? It's a display format and such conversion should be the last to be done, ideally by the template. Maybe, if possible, you should consider to fix the code before, instead of applying a patch like this one.

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

This will convert your string to a float:

$price = (float)substr("$455.00", 1);
echo($price);

For more information, you can see this answer, which has a couple of good links for you in it.

Upvotes: 0

Sudantha
Sudantha

Reputation: 16214

I think that your ECommerce site only has $ (USD)

$price= substr($string_price,1);

Upvotes: 0

Chris Bornhoft
Chris Bornhoft

Reputation: 4301

If you need only the dollar sign removed, use str_replace. To convert that to int or float, typecast it. However, using float results in non-exact calculations so be careful with it!

$newval = (int)str_replace('$', '', '$455.00');

Upvotes: 0

Related Questions