Ruby is adding wierd? Possibly rounding

So I am new to Ruby and also programming. This is my second script,
making an if else elsif script.

So, I have 9.99 + 17.6. It should come out to 27.59, which is my elsif
part. It should trigger that argument thing.

But, it doesn’t. So I forced Ruby to show me it’s math. And, apparently,
9.99 + 17.6 = 27.5900000000003? That’s not right. And because of that
wrong addition done by Ruby, my elsif isn’t triggering.

I suspect some sort of rounding thing with Ruby. But, how do I get
around this? Thanks – I tried looking it up but I couldn’t understand.

It’s not Ruby. It’s so in every programming language, you have to use
advanced classes to do that accurately. Read this

I suspect some sort of rounding thing with Ruby. But,
how do I get around this?

If the display is what is bothering you, you can use sprintf()
or .round() and related methods to get what you want:

# Store your original result in a variable called result
result = 9.99 + 17.6

# display it now properly
e result.round(3)

Output will be:

27.59

Also have a look at Kernel %

'%.3f' % '3.0'.to_f # => '3.000'

See that the last will pad/append ‘0’ characters.

You are using the wrong datatype for your problem. 9.99 is of type
Float, and hence can’t be used for precise arithmetic. Ruby comes with a
class BigDecimal, which does what you need. You have to require it in
your code:

require 'bigdecimal'

Have a look at the Ruby docs, how to use it.