Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
Rounding up to the next .5
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Justin C, post: 4837256"] Not strictly a perl problem, I'm just happen to be using perl to do this. I'm writing a utility for work that will calculate the price to send a parcel. The courier charges by the half kilo and they never round down. I'm trying to round weights (with up to two decimal places) up to the next half kilo (unless the weight is an exact or .5 kilo already). I've looked at the 'round' in the docs. You can round, for example, four decimal places to three, two, one, or none, but there's no mention of rounding, say, .3 to .5. Things look OK with: $weight = ( int( $kilos + .4 ) *2 ) / 2 ; Those work until you go to two decimal places, which our scales do (there is also something called dimensional weight - if your parcel takes up too much space for how much it weighs, they give it a weight appropriate for it's size and charge you that instead - we often have to go to two decimal places). I've got close with this: $weight = ( int( $kilos + .455 ) * 2 ) / 2 ; But it's not right, the first fifty grammes over the kilo/half-kilo get rounded down. Any suggestions on how I might tackle this? Thank you for any help you can give. One last thought, I could use a spreadsheet to create one huge hash, with all the weights from .01kg up to, say, 200kg. The weights being the keys and the values being the rounded weight... but there *has* to be a better way... doesn't there? Justin. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
Rounding up to the next .5
Top