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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.