# 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!

On Sep 11, 2011, at 5:05 AM, Kane W. 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!

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

Dan N.

Dan N. wrote in post #1021235:

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

Dan N.

Would you mind explaining the

“%.2f”
part? What does the % do? And what does the f do? Thanks!

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

Dan N. wrote in post #1021235:

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

Dan N.

Would you mind explaining the

“%.2f”
part? What does the % do? And what does the f do? Thanks!

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

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

Joe

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

2011/9/11 Joe C. [email protected]:

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.

You should update to Ruby 1.9.2.

– Matma R.

2011/9/11 Bartosz Dziewoński [email protected]:

2011/9/11 Joe C. [email protected]:

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.

Class: Float (Ruby 1.8.7)

You should update to Ruby 1.9.2.

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

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

Class: Float (Ruby 1.8.7)

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”

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

Joe

On Thu, Sep 15, 2011 at 4:47 PM, joe c. [email protected] wrote:

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

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