Newby - how to round up floating point number?

Discussion in 'Ruby' started by Dermot Moynihan, Dec 26, 2006.

  1. Hi guys
    Got a Ruby book yesterday from Santa, and am a bit stuck ;)

    Searched the pickaxe and some Perl books (left over after an earlier
    failed attempt at programming), and this list and Googled and found
    nothing useful.

    Some examples might make the question clear:

    Let's say that after a calculation I get 4.1200056 and I want to round
    it up to 5.
    When the calculation comes to a number like 4.0 I want it left at 4.

    The way I've been dealing with it is that after I get a figure like,
    let's say, 7.312456 I turn it into an integer (it then becomes 7) and
    then I add 1 getting, in this case, the round number 8.

    This works fine except on the occasions when the answer to the
    calculation is already a round number like, let's say 6 or 6.0. Turning
    it into an integer keeps it 6 but adding 1 makes it a 7. In such a case
    I need it to stay at 6.

    So, when it's got some figures (other than a single zero) after the
    decimal point I want to round it up to a whole number (no decimal point)
    but when it has only a single zero after the decimal point I want it
    left alone except to have the decimal point and the zero disappear.

    Sorry if there is a lack of clarity.

    Appreciate any help

    all the best
    Dermot
     
    Dermot Moynihan, Dec 26, 2006
    #1
    1. Advertising

  2. Dermot Moynihan

    Jeroen Budts Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On 2006-12-26 14:00, Dermot Moynihan wrote:
    > Let's say that after a calculation I get 4.1200056 and I want to round
    > it up to 5.
    > When the calculation comes to a number like 4.0 I want it left at 4.


    Hi!

    You can use the 'ceil'-method:

    (4.1).ceil # => 5
    (4.7).ceil # => 5
    (4.0).ceil # => 4

    fyi: there is also the floor method which always returns the lower number:
    (4.1).floor # => 4
    (4.7).floor # => 4
    (4.0).floor # => 4


    greetz!
    Jeroen
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFFkR8bH04wF4t7d0oRAsMsAJ4pKs5RXexGLwsCOdnSv/nuHX1cwQCggUKg
    GIwygWGnNvhQ0ZLJxFWYOgU=
    =0HKe
    -----END PGP SIGNATURE-----
     
    Jeroen Budts, Dec 26, 2006
    #2
    1. Advertising

  3. Dermot Moynihan <> writes:

    > Let's say that after a calculation I get 4.1200056 and I want to round
    > it up to 5.
    > When the calculation comes to a number like 4.0 I want it left at 4.
    >
    > The way I've been dealing with it is that after I get a figure like,
    > let's say, 7.312456 I turn it into an integer (it then becomes 7) and
    > then I add 1 getting, in this case, the round number 8.
    >
    > This works fine except on the occasions when the answer to the
    > calculation is already a round number like, let's say 6 or 6.0. Turning
    > it into an integer keeps it 6 but adding 1 makes it a 7. In such a case
    > I need it to stay at 6.
    >
    > So, when it's got some figures (other than a single zero) after the
    > decimal point I want to round it up to a whole number (no decimal point)
    > but when it has only a single zero after the decimal point I want it
    > left alone except to have the decimal point and the zero disappear.


    (3.5).floor

    --
    David Kastrup, Kriemhildstr. 15, 44793 Bochum
     
    David Kastrup, Dec 26, 2006
    #3
  4. Thanks Jeroen.

    Been doing a bit of reading on 'ceil' and 'floor' and am wondering if
    you have to know the actual number you are going to be working on in
    order to use them. Because I don't know what that number will be. Did
    you skim read that post - I gave some examples to try to make that
    point. Or have I just misunderstood how to use 'ceil' or 'floor'.

    I would have thought this would be a fairly mundane task. After all if
    one wants 1 and a bit ice creams or whatever, he must get two not one.
    So, rounding up is surely pretty important.

    Been experimenting with a thing called 'round' but that rounds up if the
    figure after the decimal point is .5 or greater, otherwise it rounds
    down. But I need it always UP if there is greater than zero after the
    decimal.

    Just to sum up again: what I'm looking for is a way of always rounding
    up a floating point number that arrives after some calculations. The
    answer will vary as a result of the figures used in the calculation varying.
    e.g. if the final number is 6.1234 I want it rounded up to 7.
    If the final number is 23.025875 I want it rounded up to 24.
    However, if 17.0 then have it stay 17.
    Or if 21.0 stay 21. And so on.

    I suppose what I need is some way of saying:
    IF the answer is a floating point decimal number round it up.
    If there is nothing after the decimal point (e.g. 46.0) leave it alone (46).

    Thanks for your help.

    Dermot


    Jeroen Budts wrote:
    > On 2006-12-26 14:00, Dermot Moynihan wrote:
    >>> Let's say that after a calculation I get 4.1200056 and I want to round
    >>> it up to 5.
    >>> When the calculation comes to a number like 4.0 I want it left at 4.

    >
    > Hi!
    >
    > You can use the 'ceil'-method:
    >
    > (4.1).ceil # => 5
    > (4.7).ceil # => 5
    > (4.0).ceil # => 4
    >
    > fyi: there is also the floor method which always returns the lower number:
    > (4.1).floor # => 4
    > (4.7).floor # => 4
    > (4.0).floor # => 4
    >
    >
    > greetz!
    > Jeroen
     
    Dermot Moynihan, Dec 26, 2006
    #4
  5. Dermot Moynihan

    Phrogz Guest

    Dermot Moynihan wrote:
    > Been doing a bit of reading on 'ceil' and 'floor' and am wondering if
    > you have to know the actual number you are going to be working on in
    > order to use them.


    Not at all.
    x = 3.2
    y = 15.7 * x
    p y, y.ceil
    #=> 50.24
    #=> 51


    > I would have thought this would be a fairly mundane task. After all if
    > one wants 1 and a bit ice creams or whatever, he must get two not one.
    > So, rounding up is surely pretty important.


    You're right, it is mundane. Ruby provides convenient methods for
    making the mundane easy.


    > I suppose what I need is some way of saying:
    > IF the answer is a floating point decimal number round it up.
    > If there is nothing after the decimal point (e.g. 46.0) leave it alone (46).


    As Jeroen pointed out, ceil is the answer here.
     
    Phrogz, Dec 26, 2006
    #5
  6. Dermot Moynihan wrote:
    > Thanks Jeroen.
    >
    > Been doing a bit of reading on 'ceil' and 'floor' and am wondering if
    > you have to know the actual number you are going to be working on in
    > order to use them. Because I don't know what that number will be. Did
    > you skim read that post - I gave some examples to try to make that
    > point. Or have I just misunderstood how to use 'ceil' or 'floor'.
    >

    No, you don't need to know the number in advance.

    Here's the doc for the ceil method. Look at the first two examples. Can
    you think of any numbers for which it will not do what you need?

    ruby$ ri Float#ceil
    ------------------------------------------------------------- Float#ceil
    flt.ceil => integer
    ------------------------------------------------------------------------
    Returns the smallest Integer greater than or equal to flt.

    1.2.ceil #=> 2
    2.0.ceil #=> 2
    (-1.2).ceil #=> -1
    (-2.0).ceil #=> -2
     
    Timothy Hunter, Dec 26, 2006
    #6
  7. Thanks Jeroen, Phrogz, Jason, Timothy and Gaurav and anybody who took
    the time to read that post. Got it sorted. As emphasised (sometimes I
    need a thing beaten into me), 'ceil' did the trick.

    Gaurav, interestingly, if I try your suggestion 10.0000000000000001
    still gives me 10.
    Removing one of the zeros gives me the required 11. Must be to do with
    the number of places after the decimal point that are counted. Using
    SciTE rather than irb it seems to see even less places. I doubt that it
    would ever be an issue but good to know anyway.
    Thanks for your efforts. Been playing around with what you wrote and
    trying to understand what you did. Very interesting.

    Learned a lot from all your posts. Even that p can be written instead of
    puts!

    Wonderful support you give here.
    All the best
    Dermot

    gaurav bagga wrote:
    > hi,
    >
    > this might help
    > hope this is what you want and if i understood your point
    > just beautify the method try
    >
    > irb(main):008:0> def try(x)
    > irb(main):009:1> if Integer(x)==x
    > irb(main):010:2> p x
    > irb(main):011:2> else
    > irb(main):012:2* x=Integer(x)+1
    > irb(main):013:2> p x
    > irb(main):014:2> end
    > irb(main):015:1> end
    > => nil
    > irb(main):016:0> try(10.0001)
    > 11
    > => nil
    > irb(main):017:0> try(10.0)
    > 10.0
    > => nil
    > irb(main):018:0> try(10)
    > 10
    > => nil
    > irb(main):019:0>
    >
    > regards
    > gaurav v bagga
    >
     
    Dermot Moynihan, Dec 26, 2006
    #7
  8. Dermot Moynihan

    Phrogz Guest

    Dermot Moynihan wrote:
    > Learned a lot from all your posts. Even that p can be written instead of
    > puts!


    For what it's worth, the 'p' method spits out the result of calling
    #inspect on each argument, while the 'puts' method spits out the result
    of calling #to_s on each argument.

    For numbers, the two are identical; not so for other beasts. For
    example:
    puts "Hello"
    #=> Hello

    p "Hello"
    #=> "Hello"

    puts ['a',1,false,/hello/]
    #=> a
    #=> 1
    #=> false
    #=> (?-mix:hello)

    p ['a',1,false,/hello/]
    #=> ["a", 1, false, /hello/]

    For many objects, the result of calling #inspect is a string that is
    more like source code (though it doesn't have to be) and generally
    gives you a better idea of the structure of the object.
     
    Phrogz, Dec 26, 2006
    #8
  9. Dermot Moynihan

    bbiker Guest

    Dermot Moynihan wrote:
    > Thanks Jeroen.
    >
    > Just to sum up again: what I'm looking for is a way of always rounding
    > up a floating point number that arrives after some calculations. The
    > answer will vary as a result of the figures used in the calculation varying.
    > e.g. if the final number is 6.1234 I want it rounded up to 7.
    > If the final number is 23.025875 I want it rounded up to 24.
    > However, if 17.0 then have it stay 17.
    > Or if 21.0 stay 21. And so on.
    >
    > I suppose what I need is some way of saying:
    > IF the answer is a floating point decimal number round it up.
    > If there is nothing after the decimal point (e.g. 46.0) leave it alone (46).
    >
    > Thanks for your help.
    >
    > Dermot
    >

    To me it appears that what you wan is ceil ... it alway rounds up
    1.000000001.ceil => 2
    1.0.ceil => 1

    To round down, use floor
    1.99999999.floor => 1
    2.0.floor => 2

    there's also round ... which rounds up if the fractional part is >=
    0.5 otherwise rounds down
    2.5.round => 3
    2.49999999.round => 2

    note that ceil, floor, and round have no effect on integer

    5.ceil => 5 5.floor => 5, 5.round => 5

    pick the method whose behavior you want
     
    bbiker, Dec 27, 2006
    #9
  10. Thanks bbiker. Also, further thanks to Gaurav and Phrogz, for supplying
    additional very welcome pointers.
    Problem sorted and put to bed.
    All the very best
    Dermot

    bbiker wrote:
    > Dermot Moynihan wrote:
    >> Thanks Jeroen.
    >>
    >> Just to sum up again: what I'm looking for is a way of always rounding
    >> up a floating point number that arrives after some calculations. The
    >> answer will vary as a result of the figures used in the calculation varying.
    >> e.g. if the final number is 6.1234 I want it rounded up to 7.
    >> If the final number is 23.025875 I want it rounded up to 24.
    >> However, if 17.0 then have it stay 17.
    >> Or if 21.0 stay 21. And so on.
    >>
    >> I suppose what I need is some way of saying:
    >> IF the answer is a floating point decimal number round it up.
    >> If there is nothing after the decimal point (e.g. 46.0) leave it alone (46).
    >>
    >> Thanks for your help.
    >>
    >> Dermot
    >>

    > To me it appears that what you wan is ceil ... it alway rounds up
    > 1.000000001.ceil => 2
    > 1.0.ceil => 1
    >
    > To round down, use floor
    > 1.99999999.floor => 1
    > 2.0.floor => 2
    >
    > there's also round ... which rounds up if the fractional part is >=
    > 0.5 otherwise rounds down
    > 2.5.round => 3
    > 2.49999999.round => 2
    >
    > note that ceil, floor, and round have no effect on integer
    >
    > 5.ceil => 5 5.floor => 5, 5.round => 5
    >
    > pick the method whose behavior you want
    >
    >
    >
     
    Dermot Moynihan, Jan 6, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    floating point round in C++

    John Smith, Dec 14, 2003, in forum: C++
    Replies:
    2
    Views:
    40,772
    Juergen Heinzl
    Dec 14, 2003
  2. Will Rocisky
    Replies:
    7
    Views:
    365
    Mensanator
    Aug 9, 2008
  3. ma740988
    Replies:
    13
    Views:
    1,068
    James Kanze
    Dec 14, 2008
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,374
    Saraswati lakki
    Jan 6, 2012
  5. Lucky Nl
    Replies:
    2
    Views:
    119
    Benoit Daloze
    May 26, 2010
Loading...

Share This Page