开发者

Calculating total price from string value in php

开发者 https://www.devze.com 2023-04-12 06:51 出处:网络
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.

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?

开发者_StackOverflow中文版

I hope I made myself clear...


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


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');


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

$price= substr($string_price,1);


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.


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.


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.


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: http://se2.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号