Jump to content

code will not SUM negative number in mysql field? :( pls help!)


tobitimac

Recommended Posts

 

I have a code to sum up a field called quantityHand in MySQL, in my example below, the value always return 40 as a positive number instead of 40.00-

 

0.00

0.00

40.00-

 

I have used intval() or floatval() function to convert the type of variable from string to int/float but still not working. Any help please?

 

Here is the code

 

$sql = mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHand FROM inventory where itemNumber like '%$term%' GROUP BY `itemNumber` ORDER BY `itemNumber`");


while ($row = mysql_fetch_array($sql)){
  	echo "<b>";
echo "</td><td style=\"text-align: center;\"><b>";
echo $row['itemNumber'];
echo "</td><td style=\"text-align: center;\"><b>";
echo $row['itemDesc'];
//echo "</td><td style=\"text-align: right;\">";
echo "</td><td style=\"text-align: center;\"><b>";
echo $row ['quantityHand'];
echo "</td></tr>";
    echo "</b>";

 

 

 

 

Link to comment
Share on other sites

What data type have you set the quantityHand field to? It you have set to INT then it'll remove anything after the decimal point. If you are storing numbers such as 12.99 then you need to set the data type to FLOAT instead.

 

Or you can format $row['quantityHand'] as a float, eg

echo sprintf('%01.2f', $row['quantityHand']);

Link to comment
Share on other sites

What data type have you set the quantityHand field to? It you have set to INT then it'll remove anything after the decimal point. If you are storing numbers such as 12.99 then you need to set the data type to FLOAT instead.

 

Or you can format $row['quantityHand'] as a float, eg

echo sprintf('%01.2f', $row['quantityHand']);

 

Thanks for your reply. i used VARCHAR for quantityHand in my MySQL after using echo sprintf('%01.2f', $row['quantityHand']); and getting 40.00 instead of 40.00- because in my MySQL i have 40.00-

 

Please help.

Link to comment
Share on other sites

I'm pretty sure there are very few written languages and zero programming languages that use a trailing - sign on negative numbers.

 

You need to store your numbers with a leading negative sign, i.e. -40.0

 

Thanks am looking into my data now i think the problem might be because i have the data as 40.00- insteade of -40.00

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.