This is a discussion on Adding discount script... within the PHP Language forums, part of the PHP Programming Forums category; All, I have been working on a small script that will detect the day of the month, and apply that ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
All,
I have been working on a small script that will detect the day of the month, and apply that percent off of an item. For example, on the 3rd, 3% off, 12th, 12% off, etc. This is what I have so far, does it look correct ? $t = (intval(date("j")) / 10); $p = explode(".", $price); // $price is returned from the db in this format 10.00 $np = number_format($p[0] - $t, 2); Any improvements ? Thanks. |
|
|||
|
*** StinkFinger escribió/wrote (Sat, 4 Sep 2004 12:52:20 -0800):
> $p = explode(".", $price); // $price is returned from the db in this format > 10.00 If you want to truncate the price just use (int)$price in your formula. I just wonder why you don't round(). -- -- -+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716 +- http://www.demogracia.com (la web de humor para mayores de 100 años) ++ «Sonríe, que te vamos a hacer una foto para la esquela» -- |
|
|||
|
Thanks. I've reworked the code and came up w/this:
//$p is returned from a db like this 10.00 $np = number_format((intval($p) - (intval(date("j")) / 10)), 2); echo $np; //returns 9.60 (for today) I cannot figure out either how or why I would use round(). Can you provide an example ? Thanks. "Alvaro G. Vicario" <kAlvaroNOSPAMTHANKS@terra.es> wrote in message news:ajh7z5rzrc7h$.v0nn7qif0pk0$.dlg@40tude.net... > *** StinkFinger escribió/wrote (Sat, 4 Sep 2004 12:52:20 -0800): >> $p = explode(".", $price); // $price is returned from the db in this >> format >> 10.00 > > If you want to truncate the price just use (int)$price in your formula. I > just wonder why you don't round(). > > -- > -- > -+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716 > +- http://www.demogracia.com (la web de humor para mayores de 100 años) > ++ «Sonríe, que te vamos a hacer una foto para la esquela» > -- |
|
|||
|
*** StinkFinger escribió/wrote (Sat, 4 Sep 2004 15:02:21 -0800):
> I cannot figure out either how or why I would use round(). > Can you provide an example ? If the price is 25.99 and you truncate 25, you carry an error of 99 cents. If you round to 26, the error is 1 cent. Rounding, the error is never larger than 50 cents. -- -- -+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716 +- http://www.demogracia.com (la web de humor para mayores de 100 años) ++ «Sonríe, que te vamos a hacer una foto para la esquela» -- |
|
|||
|
> If the price is 25.99 and you truncate 25, you carry an error of 99 cents.
> If you round to 26, the error is 1 cent. Rounding, the error is never > larger than 50 cents. Ok, I see now. Thanks for pointing that out. In my case, all my prices are even dollar amounts, but I will keep that in mind if I ever need to change it. Thanks again. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|