Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Different way of rounding

by Anonymous Monk
on Dec 07, 2002 at 02:18 UTC ( #218202=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to round in a more different way

If I had 1.2818, instead of it rounding to 1.28, I need it to round to 1.29. So I always need it to round to the second decimal point. Heres some examples.
1.1213 = 1.13 234.33 = 234.33 32.1323 = 32.14 55.6274 = 55.63
Please find a way to do this, thank you.

Replies are listed 'Best First'.
Re: Different way of rounding
by BrowserUk (Pope) on Dec 07, 2002 at 10:18 UTC

    You don't mention how you wish to treat negative values? The problem with POSIX::ceil and other methods offered so far is that they round positive numbers towards infinity and negative numbers towards zero.

    In many calculations, if you expect the result of two similar calculations done with negative and positive values to balance each other (eg. debit + credit = 0), then these methods will result in erroneous results.

    If this is important to your application you should round all your values in the same absolute direction. That is to say, if you are rounding your positive values away from zero (ie. up) then you should also round your negative values away from zero.

    sub ceil_2dp($) { my $num=shift()*1e2; # x 100 return int($num) == $num # It had exactly 2dp. ? $num/1e2 # return it as it was : $num < 0 # less than zero ? int(--$num)/1e2 # -1 int and div : int(++$num)/1e2; # else +1 and div } for (1.2818, 1.1213, 234.33, 32.1323, 55.6274, -1.2818, -1.1213, -234. +33, -32.1323, -55.6274) { print ceil_2dp( $_ ) . $/; } __END__ 1.29 1.13 234.33 32.14 55.63 -1.29 -1.13 -234.33 -32.14 -55.63

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Different way of rounding
by tstock (Curate) on Dec 07, 2002 at 02:35 UTC
    I would multiple by 100, then use the POSIX::ceil function.
    use POSIX qw(ceil); my $rounded = ceil($i*100) / 100;
Re: Different way of rounding
by tachyon (Chancellor) on Dec 07, 2002 at 03:41 UTC
    my @nums = ( 1.2818, 1.1213, 234.33, 32.1323, 55.6274 ); print ceil($_), "\n" for @nums; sub ceil { my $num = shift; $num *= 100; $num = $num == int( $num ) ? $num : int(++$num); return $num/100; } __DATA__ 1.29 1.13 234.33 32.14 55.63




Re: Different way of rounding
by atcroft (Monsignor) on Dec 07, 2002 at 02:31 UTC

    Interesting problem... perhaps you could multiply the number by 1000, mod by 10, and if true, then add 10, div by 10 (get integer only), then divide by 100. Not the best of ways, but logically would seem to work....

    Sample 1: 1.1213

    1. 1.1213 * 1000 = 1121.3
    2. 1121.3 % 10 = 1
    3. 1121.3 + 10 = 1131.3
    4. 1131.3 / 10 = 113
    5. 113 / 100 = 1.13

    Sample 2: 234.33

    1. 234.33 * 1000 = 234330
    2. 234330 % 10 = 0
    3. 234330 / 10 = 23433
    4. 23433 / 100 = 234.33
Re: Different way of rounding
by pg (Canon) on Dec 07, 2002 at 06:52 UTC
    As you are trying to round to the second decimal, it makes me thinking that, you might be dealing with amount of money. If this is the case, I want to remind you one thing, Perl just like c, its prime types only support floating decimals, not fixed ones. You have to be prepared for some sort of gray areas for errors.

    If you are serious about accuracy and precision, better use something like Math::BigFloat.
Re: Different way of rounding
by fruiture (Curate) on Dec 07, 2002 at 09:48 UTC
    Please find a way to do this

    Are you assigning tasks or do you want help?

    (as you've been told, POSIX::ceil() is the hint you need.)

(bbfu) (perlfaq) Re: Different way of rounding
by bbfu (Curate) on Dec 07, 2002 at 22:10 UTC

    > perldoc -q round Found in C:\Perl\lib\pod\perlfaq4.pod Does Perl have a round() function? What about ceil() and floor()? +Trig functions? Remember that int() merely truncates toward 0. For roundin +g to a certain number of digits, sprintf() or printf() is usually + the easiest route. printf("%.3f", 3.1415926535); # prints 3.142

    Update: Ah, my mistake. You always want it to round up. How odd. :) Yeah, see BrowserUK's solution.

    Black flowers blossum
    Fearless on my breath

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://218202]
Approved by Zaxo
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree
[choroba]: Playing in a punk rock band for 20 years... my hearing is quite bad
[Corion]: I still have hopes to turn my godson and his two siblings into a punk band ;)
[Corion]: Their older sister just started piano but has been interested in drumming, which she should be able to start with 8 years or so)
[Discipulus]: ah was Kink of Bongo 1992..
[1nickt]: choroba name of band? youtube link? MySpace link?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-03-24 12:21 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.