J
Justin C
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.
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.