How do I make output generate a float without an excess numbers of decimal places?

For example, my current code is

def grow days
   while days != 0
     growheight = rand(5) / 100.0
     @height = @height + growheight
     days = days - 1
   end
end

If days is a large number, say a year, it usually ends up with a number
like 7.4799999999999999984. Is there a way to fix this? Perhaps chopping
off after 2 decimal places in a string? Thanks in advance!

···

--
Posted via http://www.ruby-forum.com/\.

...

If days is a large number, say a year, it usually ends up with a number
like 7.4799999999999999984. Is there a way to fix this? Perhaps chopping
off after 2 decimal places in a string? Thanks in advance!

You might try using the % instance method of String.
For example:
"%.2f" % (1.0/3) => "0.33"

Dan Nachbar

···

On Sep 11, 2011, at 5:05 AM, Kane Williams wrote:

If days is a large number, say a year, it usually ends up with a number
like 7.4799999999999999984. Is there a way to fix this? Perhaps chopping
off after 2 decimal places in a string? Thanks in advance!

This is not as detailed but it may be worth a look.

n = 7.4799999999999999984
p n.round(2) #> 7.48
p n.round(1) #> 7.5

http://www.ruby-doc.org/core/classes/Float.html#M000144

Harry

I tried n.round(2) and gave an error - didnt want the 2 argument. I am
using an older version of ruby?

Joe

···

--
Posted via http://www.ruby-forum.com/.

roger....that class is clever and it works great - thanks!

Joe

···

--
Posted via http://www.ruby-forum.com/.

Dan Nachbar wrote in post #1021235:

You might try using the % instance method of String.
For example:
"%.2f" % (1.0/3) => "0.33"

Dan Nachbar

Would you mind explaining the

"%.2f"

part? What does the % do? And what does the f do? Thanks!

···

--
Posted via http://www.ruby-forum.com/\.

In 1.8.x, round() simply rounded to nearest integer.

You should update to Ruby 1.9.2.

-- Matma Rex

···

2011/9/11 Joe Collins <joec_49@hotmail.com>:

I tried n.round(2) and gave an error - didnt want the 2 argument. I am
using an older version of ruby?

Apart from that it's not rounding. It's just a convenience way to
format a Float into a String. Really, usually you don't want to round
numbers but you want a specific output formatting.

Kind regards

robert

···

On Thu, Sep 15, 2011 at 4:47 PM, joe c. <joec54@gmail.com> wrote:

roger....that class is clever and it works great - thanks!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

There are quite a few online resources that cover the details
much better than I can. A quick google search turns
up several. For example, see

The formats are the same as the ones used by sprintf and can
be found in the documentation for that method -
http://ruby-doc.org/core/classes/Kernel.html#M001432

Most books on ruby also cover this topic to some degree.

Dan Nachbar

···

On Sep 11, 2011, at 5:47 AM, Kane Williams wrote:

Dan Nachbar wrote in post #1021235:

You might try using the % instance method of String.
For example:
"%.2f" % (1.0/3) => "0.33"

Dan Nachbar

Would you mind explaining the

"%.2f"

part? What does the % do? And what does the f do? Thanks!

Also, rounding is rarely what one wants because it looses precision
and still gives no guarantee for proper output formatting. Usually
output formatting with printf, sprintf or % operator is better.

Btw, Joe if you are using floating point numbers for currency values
you might run into trouble soon because of the pecularities of IEEE
floating point math. In those cases it might be better to use
BigDecimal.

Kind regards

robert

···

2011/9/11 Bartosz Dziewoński <matma.rex@gmail.com>:

2011/9/11 Joe Collins <joec_49@hotmail.com>:

I tried n.round(2) and gave an error - didnt want the 2 argument. I am
using an older version of ruby?

In 1.8.x, round() simply rounded to nearest integer.

http://www.ruby-doc.org/core-1.8.7/classes/Float.html#M000650

You should update to Ruby 1.9.2.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In 1.8.x, round() simply rounded to nearest integer.

http://www.ruby-doc.org/core-1.8.7/classes/Float.html#M000650

You should update to Ruby 1.9.2.

or define your own round method

class Float
   def round2 n
     "%.#{n}f" % self
   end
end

1.11111.round2(2)

=> "1.11"

···

--
Posted via http://www.ruby-forum.com/\.