i have two number qty_p and price_db. qty_p will be always int. and price_db can be int or double with 2 decimal place.

$qty_p = $_POST['qty'];     //get number from input field
$price_db = $row['price'];  //get number from database

on top iam get the value and so far its fine.

but below the php is round the number up and losing the 2 decimals.

$total = $price_db * $qty_db;

i think the problem is that $price_db is a string. and than its being convert to int and not double.

i tried this but didnt worked:

 $price_db = $row['price'];
 $price_db = (float)$price_db;

Recommended Answers

All 3 Replies

Member Avatar

That's odd, even:

echo '7' * '1.34';

Gives me the right answer.

i thought its bc when i get number from database by:

$price_db = $row['price'];

this turn $price_db in to string.
than if i do:

$price_db * $qty_db;

this turn it to int.

may be that by its losing the 2 decimals

Member Avatar

So for product price, I'm assuming that you're using DECIMAL(6,2) mysql datatype or similar?

If you're not using NUMERIC or DECIMAL - then perhaps you should consider it as floats are BAD for prices.


did you try echoing these values out before multiplying, to see what they held?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.