8 Replies - 995 Views - Last Post: 17 January 2013 - 02:24 AM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

php losing 2 decimal

Posted 09 January 2013 - 10:49 PM

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'];
$price_db = $row['price'];


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. can any one explain to me why its doing that. is there a function that can i use?

$total = $price_db * $qty_db;


Is This A Good Question/Topic? 0
  • +

Replies To: php losing 2 decimal

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: php losing 2 decimal

Posted 09 January 2013 - 11:06 PM

Well, you could use a toFixed(2) function to make the decimal values to be printed...but I am not sure if you can do that in PHP.

I would suggest you write a Javascript function for the same and use the toFixed inside the Javascript.

Edit: Well, not necessary...you can use round() function for the same like this...

echo round($total,2);


regards,
Raghav

This post has been edited by raghav.naganathan: 09 January 2013 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,749
  • Joined: 08-August 08

Re: php losing 2 decimal

Posted 10 January 2013 - 09:45 AM

$price_db is a string, so when you use it as part of a calculation it gets converted to a numeric. It may be getting converted to an integer, losing the decimal value.

When dealing with money it's a good idea to do everything in pennies. For example, $10.00 would be represented by the integer: 1000 and you would divide by 100 when displaying the value.
Was This Post Helpful? 2
  • +
  • -

#4 publicFunction  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 10
  • Joined: 06-October 10

Re: php losing 2 decimal

Posted 16 January 2013 - 05:18 PM

Guys,

If you 100% know that the data coming in is going to be a number then use:
number_format($total, 2);


If you cant be sure wrap it in an if statement using:
is_numeric($var);


Like so:
if (is_numeric($total)) { number_format($total, 2); }

I would suggest that you check the posted variables to see if they are numbers. The preferred method would be via the client by using Javascript to check first, then only allow the data to be submitted if they are numbers (int, float, decimal). Once that check is passed you would not need to check those to see if they were numeric, but I would still put that in place as a safety net. Remember that you should never trust the data that is sent and always run checks on it before handing it.

if (is_numeric($_POST['qty']) && is_numeric($row['price'])) {
    $qty_p = $_POST['qty'];
    $price_db = $row['price'];
} else {
/*    Send Error
}



Was This Post Helpful? 2
  • +
  • -

#5 publicFunction  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 10
  • Joined: 06-October 10

Re: php losing 2 decimal

Posted 16 January 2013 - 05:24 PM

Sorry Folks,

Hit Enter by mistake. The example is not 100%, try this (untested):

if (is_numeric($_POST['qty']) && is_numeric($row['price'])) {
    $qty_p = $_POST['qty'];
    $price_db = $row['price'];
    $total = number_format(($price_db * $qty_db), 2);
} else {
    /*    Send Error    */
}



This should ensure that the data coming in are numbers and that they are handled properly, so no unexpected errors appear.
Was This Post Helpful? 1
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: php losing 2 decimal

Posted 16 January 2013 - 11:13 PM

Well, I believe the round() function works pretty much in the same way like I have already mentioned.

$total = $price_db * $qty_db;
 echo round($total,2);


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#7 publicFunction  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 10
  • Joined: 06-October 10

Re: php losing 2 decimal

Posted 17 January 2013 - 02:16 AM

Yes,

BUT round will effect the result of the number, the same way ceil would, I would suggest avoiding it. Its quite a sloppy approach and you should never assume you need to one or the other to any number.
Was This Post Helpful? 1
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: php losing 2 decimal

Posted 17 January 2013 - 02:21 AM

Is that so? Thank you for that information. :)

Well, I am still a beginner in PHP and I had not come across the number_format function till today.

Looks like I learned something new today, thanks to you :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#9 publicFunction  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 10
  • Joined: 06-October 10

Re: php losing 2 decimal

Posted 17 January 2013 - 02:24 AM

Your Welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1