Re: Numeric help needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 15, 2012, at 19:00, "Simon J Welsh" <simon@xxxxxxxxxxx<mailto:simon@xxxxxxxxxxx>> wrote:

On 16/01/2012, at 2:48 PM, Chris Payne wrote:

"If the loan amount is $68500.00, the insurace will be based on
$69000.00 as the amount is always rounded up to the next $1000."

The round() function only rounds decimal values. You can use this to emulate rounding to a near power of ten by dividing, rounding, then multiplying again. i.e. echo "<br>" . round(68500/1000) * 1000 . " ROUNDED";

You can also pass a second parameter to round() that indicates the precision to round to. If you pass a negative precision value, you can round to higher-order digits. For example, pass -3 to round to the nearest thousand.

Having said that, based on the quote above, I believe this would be an incorrect solution to the problem. It sounds like the value is simply always rounded up to the nearest thousand, which means round() is not the function to use as it will sometimes round down. Instead ceil() should be used, as it always rounds up to the next whole number/integer. It lacks a precision argument, however, so the OP would need to use the divide-adjust-multiple trick you provided to make it work. That is, something like this:

$newValue = ceil(68500 / 1000) * 1000

Numbers smaller than 1000 will need to be handled as an edge case, since this algorithm will "adjust" them to zero for you (same as when using round(), incidentally). If negative numbers are valid inputs, they'll also need careful review to ensure correct behavior.

Hope that helps.

--
Bob Williams

________________________________
Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux