Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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.

Comment on Different way of rounding
Download Code
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 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;
    Tiago
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

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

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.)

    --
    http://fruiture.de
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.

(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.

    bbfu
    Black flowers blossum
    Fearless on my breath

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://218202]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (79 votes), past polls