ikon Posted February 25, 2008 Share Posted February 25, 2008 I'm trying to price up products in a DB with the field type decimal - 9,2 and the default of 0.00 however, when i enter a price of 5.75 in my products table its rounding up to 6 - any ideas why anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/ Share on other sites More sharing options...
GameYin Posted February 25, 2008 Share Posted February 25, 2008 Post the code you're using. I bet I know what your doing but I want to make sure your doing what I think Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-475820 Share on other sites More sharing options...
fenway Posted February 25, 2008 Share Posted February 25, 2008 Also, the table structure, and the insert statement in question. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-475856 Share on other sites More sharing options...
ikon Posted February 26, 2008 Author Share Posted February 26, 2008 This is the product table.... Its the pd_price thats having the problem. CREATE TABLE `tbl_product` ( `pd_id` int(10) unsigned NOT NULL auto_increment, `cat_id` int(10) unsigned NOT NULL default '0', `pd_name` varchar(100) NOT NULL default '', `pd_description` text NOT NULL, `pd_price` decimal(9,2) NOT NULL default '0.00', `pd_qty` smallint(5) unsigned NOT NULL default '0', `pd_image` varchar(200) default NULL, `pd_thumbnail` varchar(200) default NULL, `pd_date` datetime NOT NULL default '0000-00-00 00:00:00', `pd_last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`pd_id`), KEY `cat_id` (`cat_id`), KEY `pd_name` (`pd_name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=402 ; Here is the call function in php that is having the problem. ?> <tr class="content"> <td width="80" align="center"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" border="0"></a></td> <td><a href="<?php echo $productUrl; ?>"><?php echo $pd_name; ?></a></td> <td align="right"><?php echo displayAmount($pd_price); ?></td> <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);"> <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>"> <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>"> </td> <td align="right"><?php echo displayAmount($pd_price * $ct_qty); ?></td> <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box"> </td> </tr> <?php } ?> Hope this helps, I've also used it here... <?php $subTotal = 0; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $pd_name = "$ct_qty x $pd_name"; $url = "index.php?c=$cat_id&p=$pd_id"; $subTotal += $pd_price * $ct_qty; ?> <tr> <td><a href="<?php echo $url; ?>"><?php echo $pd_name; ?></a></td> <td width="30%" align="right"><?php echo displayAmount($ct_qty * $pd_price); ?></td> </tr> <?php Cheers All Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477113 Share on other sites More sharing options...
luca200 Posted February 26, 2008 Share Posted February 26, 2008 ....the code for filling the table? Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477123 Share on other sites More sharing options...
aschk Posted February 26, 2008 Share Posted February 26, 2008 Also while you're find the INSERT statement for us, can you give us the code for the displayAmount() function. Can't guarantee that's not doing something crazy Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477133 Share on other sites More sharing options...
ikon Posted February 26, 2008 Author Share Posted February 26, 2008 erm, i think this is the code you mean... This is to populate the cart: function getCartContent() { $cartContent = array(); $sid = session_id(); $sql = "SELECT ct_id, ct.pd_id, ct_qty, pd_name, pd_price, pd_thumbnail, pd.cat_id FROM tbl_cart ct, tbl_product pd, tbl_category cat WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id"; this is the query to get the totoal amount for the order function getOrderAmount($orderId) { $orderAmount = 0; $sql = "SELECT SUM(pd_price * od_qty) FROM tbl_order_item oi, tbl_product p WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId UNION SELECT od_shipping_cost FROM tbl_order WHERE od_id = $orderId"; $result = dbQuery($sql); if (dbNumRows($result) == 2) { $row = dbFetchRow($result); $totalPurchase = $row[0]; $row = dbFetchRow($result); $shippingCost = $row[0]; $orderAmount = $totalPurchase + $shippingCost; } return $orderAmount; } But its the point before this thats its displaying the wrong data....This is the cart <?php if (!defined('WEB_ROOT')) { exit; } $cartContent = getCartContent(); $numItem = count($cartContent); ?> <table width="100%" border="1" cellspacing="0" cellpadding="2" id="minicart"> <?php if ($numItem > 0) { ?> <tr> <td colspan="2">Cart Content</td> </tr> <?php $subTotal = 0; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $pd_name = "$ct_qty x $pd_name"; $url = "index.php?c=$cat_id&p=$pd_id"; $subTotal += $pd_price * $ct_qty; ?> <tr> <td><a href="<?php echo $url; ?>"><?php echo $pd_name; ?></a></td> <td width="30%" align="right"><?php echo displayAmount($ct_qty * $pd_price); ?></td> </tr> <?php } // end while ?> <tr><td align="right">Sub-total</td> <td width="30%" align="right"><?php echo displayAmount($subTotal); ?></td> </tr> <tr><td align="right">Shipping</td> <td width="30%" align="right"><?php echo displayAmount($shopConfig['shippingCost']); ?></td> </tr> <tr><td align="right">Total</td> <td width="30%" align="right"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td> </tr> <tr><td colspan="2"> </td></tr> <tr> <td colspan="2" align="center"><a href="cart.php?action=view"> Go To Shopping Cart</a></td> </tr> <?php } else { ?> <tr><td colspan="2" align="center" valign="middle">Shopping Cart Is Empty</td></tr> <?php } ?> </table> This is the saving of the items ordered.... if ($orderId) { // save order items for ($i = 0; $i < $numItem; $i++) { $sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty) VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})"; $result = dbQuery($sql); } here is the displayAmount function: echo displayAmount($ct_qty * $pd_price); Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477182 Share on other sites More sharing options...
fenway Posted February 26, 2008 Share Posted February 26, 2008 Let's see the value of $sql. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477215 Share on other sites More sharing options...
ikon Posted February 26, 2008 Author Share Posted February 26, 2008 what do you mean see the value? Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477368 Share on other sites More sharing options...
fenway Posted February 26, 2008 Share Posted February 26, 2008 what do you mean see the value? I mean echo the $sql variable inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477377 Share on other sites More sharing options...
aschk Posted February 27, 2008 Share Posted February 27, 2008 echo displayAmount($ct_qty * $pd_price); Also, you still haven't posted what the displayAmount function actually looks like. Yes you posted how you're USING it, but not where it's defined. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-477896 Share on other sites More sharing options...
ikon Posted February 27, 2008 Author Share Posted February 27, 2008 I'm not too sure how do that i'm afraid..... Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478041 Share on other sites More sharing options...
aschk Posted February 27, 2008 Share Posted February 27, 2008 Ok, who wrote the script then, because without seeing what the displayAmount() function actually does it's hard to guarantee any answers we might give you. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478084 Share on other sites More sharing options...
ikon Posted February 27, 2008 Author Share Posted February 27, 2008 are you after the function of it?? function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount); } Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478118 Share on other sites More sharing options...
ikon Posted February 27, 2008 Author Share Posted February 27, 2008 common.php - this includes all the functions being called <?php /* Contain the common functions required in shop and admin pages */ require_once 'config.php'; require_once 'database.php'; /* Make sure each key name in $requiredField exist in $_POST and the value is not empty */ function checkRequiredPost($requiredField) { $numRequired = count($requiredField); $keys = array_keys($_POST); $allFieldExist = true; for ($i = 0; $i < $numRequired && $allFieldExist; $i++) { if (!in_array($requiredField[$i], $keys) || $_POST[$requiredField[$i]] == '') { $allFieldExist = false; } } return $allFieldExist; } function getShopConfig() { // get current configuration $sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_order_email, cy_symbol FROM tbl_shop_config sc, tbl_currency cy WHERE sc_currency = cy_id"; $result = dbQuery($sql); $row = dbFetchAssoc($result); if ($row) { extract($row); $shopConfig = array('name' => $sc_name, 'address' => $sc_address, 'phone' => $sc_phone, 'email' => $sc_email, 'sendOrderEmail' => $sc_order_email, 'shippingCost' => $sc_shipping_cost, 'currency' => $cy_symbol); } else { $shopConfig = array('name' => '', 'address' => '', 'phone' => '', 'email' => '', 'sendOrderEmail' => '', 'shippingCost' => '', 'currency' => ''); } return $shopConfig; } function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount); } /* Join up the key value pairs in $_GET into a single query string */ function queryString() { $qString = array(); foreach($_GET as $key => $value) { if (trim($value) != '') { $qString[] = $key. '=' . trim($value); } else { $qString[] = $key; } } $qString = implode('&', $qString); return $qString; } /* Put an error message on session */ function setError($errorMessage) { if (!isset($_SESSION['plaincart_error'])) { $_SESSION['plaincart_error'] = array(); } $_SESSION['plaincart_error'][] = $errorMessage; } /* print the error message */ function displayError() { if (isset($_SESSION['plaincart_error']) && count($_SESSION['plaincart_error'])) { $numError = count($_SESSION['plaincart_error']); echo '<table id="errorMessage" width="550" align="center" cellpadding="20" cellspacing="0"><tr><td>'; for ($i = 0; $i < $numError; $i++) { echo '• ' . $_SESSION['plaincart_error'][$i] . "<br>\r\n"; } echo '</td></tr></table>'; // remove all error messages from session $_SESSION['plaincart_error'] = array(); } } Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478125 Share on other sites More sharing options...
AndyB Posted February 27, 2008 Share Posted February 27, 2008 http://us.php.net/manual/en/function.number-format.php The number format function returns integers unless you specify how many decimal places you want. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478134 Share on other sites More sharing options...
aschk Posted February 27, 2008 Share Posted February 27, 2008 And there you have your answer then. I thought as much. Thus function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount); } should be changed to function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount,2); } Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478187 Share on other sites More sharing options...
fenway Posted February 27, 2008 Share Posted February 27, 2008 what do you mean see the value? I mean echo the $sql variable inside the loop. I'm not too sure how do that i'm afraid..... PLEASE start using code blocks... I'm getting tired to re-formatting everything. Change your "saving" block to this: if ($orderId) { // save order items for ($i = 0; $i < $numItem; $i++) { $sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty) VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})"; echo $sql; $result = dbQuery($sql); } Then post the output from the echo call. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478239 Share on other sites More sharing options...
Barand Posted February 27, 2008 Share Posted February 27, 2008 function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount, 2); } Specify the decimal places. Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478555 Share on other sites More sharing options...
ikon Posted February 28, 2008 Author Share Posted February 28, 2008 Many thanks for all your time and effort helping me with this....... I'm sorry it got a bit tedius but its now solved Thank you x Quote Link to comment https://forums.phpfreaks.com/topic/92891-decimal-is-rounding-up-to-nearest-number-i-dont-want-that/#findComment-478651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.