Newby - how to round up floating point number?

D

Dermot Moynihan

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
 
J

Jeroen Budts

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

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-----
 
D

David Kastrup

Dermot Moynihan said:
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
 
D

Dermot Moynihan

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
 
P

Phrogz

Dermot said:
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.
 
T

Timothy Hunter

Dermot said:
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
 
D

Dermot Moynihan

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
 
P

Phrogz

Dermot said:
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.
 
B

bbiker

Dermot said:
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
 
D

Dermot Moynihan

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top