Newby - how to round up floating point number?

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

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

On 2006-12-26 14:00, Dermot M. 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-----

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

Dermot M. 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.

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 M. 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``````

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

On 12/26/06, Dermot M. [email protected] wrote:

one wants 1 and a bit ice creams or whatever, he must get two not one.
varying.
Thanks for your help.

Dermot

Yeah, the ceil method always rounds a float up to the next integer
apparently.

C:\ruby\programs>irb
irb(main):001:0> miscNum = 4.0
=> 4.0
irb(main):002:0> miscNum.ceil
=> 4
irb(main):003:0> miscNum = 4.01
=> 4.01
irb(main):004:0> miscNum.ceil
=> 5

hi,
that was good to know

irb(main):001:0> o=10.0000000000000001
=> 10.0
irb(main):002:0> o.class
=> Float
irb(main):003:0> 10.0000000000000001.class
=> Float
irb(main):004:0>
10.00000000000000000000000000000000000000000000000001.class
=> Float
irb(main):005:0> 10.00000000000000000000000000000000000000000000000001
=> 10.0
irb(main):008:0>
10.6856856856856856856755555555555555555555555555555555555551
=> 10.6856856856857
irb(main):009:0>
10.6856856856856156856755555555555555555555555555555555555551
=> 10.6856856856856
irb(main):010:0>

never thought what happens when floats get this way "
10.00000000000000000000000000000000000000000000000001"

regards
gaurav

hi,

if you get such numbers in your calculations then

irb(main):012:0> 10.0000000000000001.ceil
=> 10
irb(main):013:0>

10.0000000000000001 i copied what you supplied

anyone experience enough in ruby can you explain how ruby handles floats
i
bean big floats
this thread has raised doubts in my mind

iāll appreciate if any one replies to this query of mine

regards
gaurav

hi,

i went through Floating Point - Wikipedia got answer

regards
gaurav

Dermot M. 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.

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

Dermot M. wrote:

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