Ruby Returns Incorrect Integer Or Decimal Places When Dividing Two Numbers

Posted By Weston Ganger

I was noticing a weird issue in ruby where I had a division statement that was returning an integer when it should have returned a decimal value. Check this out.

number = 0.1 + BigDecimal('0.2') # Note the value passed to BigDecimal is a string, this is important to maintain accuracy
number.to_s('F')  #=> 0.3     '# F' specifies using floating point notation instead of engineering

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:August 11, 2016