0

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;
2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by diafol
0

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

0

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.

//EDIT

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

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.