Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:03+00:00 2026-06-11T10:46:03+00:00

I learned that it’s recommended to use BigDecimal instead of Float , but this

  • 0

I learned that it’s recommended to use BigDecimal instead of Float, but this one is either a bug or highlights the esoteric nature of Float. It seems that Float#round(2) has a problem with “1.015”, “1.025” and “1.035”.

1.015.round(2)
 => 1.01    # => WRONG .. should be 1.02
1.025.round(2)
 => 1.02    # => WRONG .. should be 1.03
1.035.round(2)
 => 1.03    # => WRONG .. should be 1.04
1.045.round(2)
 => 1.05    # => CORRECT
1.016.round(2)
 => 1.02    # => CORRECT

round(3) works fine.

1.0015.round(3)
 => 1.002  # => CORRECT
1.235.round(2)
 => 1.24   # => CORRECT 

To monkey patch this in a Rails app, I did this:

config/initializers/float_mp.rb

require 'bigdecimal'

class Float
  def round(val=0)
     BigDecimal.new(self.to_s).round(val).to_f
  end
end

This seems to be a weird and expensive work-around. Could this be a bug in Float#round?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:46:04+00:00Added an answer on June 11, 2026 at 10:46 am

    AFAICS the ruby round() works correctly. Presumably it’s just a wrapper around the round() function in libm anyway.

    So the reason is that your floating point literals cannot be represented exactly in binary. E.g. “1.015” printed with a few more decimals gives “1.0149999999999999”; thus when rounding to two decimal digits, 1.01 is closer to the true value than 1.02. And so on for your other examples as well.

    Also keep in mind that the default IEEE 754 rounding mode is “Round to nearest, ties to even” which is not the same as “Round to nearest, ties away from zero” which is what you may be familiar with from school.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learned that the name attribute has been deprecated. But this is how I
Today I learned that one of the .NET libraries I use, OpenTK, requires me
I learned that function toggle() is deprecated. So what can i use instead toggle
I learned that when executing commands in Python, I should use subprocess. What I'm
I've just learned that we can iterate through all checkboxes in a document, but
I learned that Sessions are no longer the recommended way to preserve data in
Just today learned that one of my websites, TwitPeek.net, is not rendering properly in
I just learned that I could use chmod make myscript.sh executable and the run
I learned that class fields are stored in the heap, but where are methods
I just learned that OCAML have to have a . postfix for doing float

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.