to_f vs to_d in Ruby

My girlfriend asked me about the difference between to_f (to float) and to_d (to decimal) in Ruby. By the way, the method to_d is a support method from the ruby library “bigdecimal/util”

I figured that the bigdecimal library provides a more precise decimal number than floats. But it was not all.

After some exploration she explained to me that while it is true that to_d is more precise the main difference from to_f is that it does not work with nil.

require 'bigdecimal'
require 'bigdecimal/util'

nil =

nil.to_f #=> 0.0

nil.to_d # => NoMethodError: undefined method `to_d' for nil:NilClass

12.to_d # 12.0

She continues explaining to me that this main reason why she needs to use float not decimal in her code (for example for when a user has not provided a number).

I love learning from her 🙂

13 thoughts on “to_f vs to_d in Ruby

  1. Good Day,

    Thank you for sharing the coding information.

    I was wondering if you might be able to advise on a coding solution i need on my website? Please let me know.


    Kirsten Slater.

      1. Awesome, thanks!

        Well what I’m looking for is to automate a ranking system of users, based on how frequently that user’s name appears on a certain page of my site (as they contribute to the site). So first I need a crawl to count the frequency of each users name on this page, and then hash it as an array or a similar solve, where the most frequently occurring user name is max (ranked 1) and least occurring user name is min.

        Is this something straight forward to code? Please advise, much appreciated.



      2. Yep that sounds straightforward. Are you learning coding at the moment? I can help you with the web scraping of your page. It is fun actually and I encourage you to learn it as well. What page is that?

  2. Thanks so much, I was doing self-taught coding at one stage but I didn’t practice enough to keep my skills up.

    So basically I run a crowd funding website, where users fund other users projects.

    What I’d like, is for those who fund projects to be ranked according to how many projects they have contributed toward financially (and not ranked on the dollar amount, but just how many projects).

    So if you invest $1 in 8 projects, and I invest $200 in 5 projects, you will still be ranked number #1 on the leaderboard and I will be #2 because you have helped more people than me. So basically it’s the funding page only that I want the frequency to count.

  3. Thanks. So the page that has all the projects is as follows:

    If you go into one of the projects for example, there will be a ‘backer’ tab to see all the names of those who invested in that project:

    So I want all the backers across all projects to be ranked. Based on how many projects they have contributed to. I want the max to min frequency of user names to then be ranked on the homepage:

    Thanks again for checking it out 🙂

    1. Hey, I just got your message on my site. Yea i know it’s not the best, but i’m not a coder so i did what i could with what i had. I found a way to parse the site though, which seems to be working ok 🙂

  4. Not the greatest idea. This can be solved using ||. For example:
    amount = (user_amount || 0).to_d
    in case user_amount was nil, 0 will be used instead.

Leave a Reply to Dr Slater Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s