0

I am having problems with a variable which reads 17,50 for calculation. The variable is part of an array and i am just getting the value 17.00

$product_price_tmp = intval(str_replace(",",".",$custom_values['product_price'][0]));

and i tried the following

$product_sub_total = sprintf("%.2f",$product_price_tmp);//reads 17.00
$product_sub_total = $product_price_tmp;//reads 17

I didn't notice the problem as all my test price values were rounded numbers.

Any tips?

4
  • 1
    why are you using intval() if you want a float? Commented May 18, 2015 at 8:13
  • Have you tried without intval()? Commented May 18, 2015 at 8:13
  • 2
    Use floatval() instead of intval() Commented May 18, 2015 at 8:16
  • Arrrgggh, totally overlooked intval vs floatval, thanks guys (girls) Commented May 18, 2015 at 8:22

2 Answers 2

2

Replace this

$product_price_tmp = intval(str_replace(",",".",$custom_values['product_price'][0]));

with this

$product_price_tmp = floatval(str_replace(",",".",$custom_values['product_price'][0]));
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need intval since it is not int you intend to get. It is enough to cast float type here:

$product_price_tmp = (float) str_replace(",",".",$custom_values['product_price'][0]);
var_dump($product_price_tmp);

//float 17.5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.