Ruby Number Helpers

Barak Saidoff
2 min readDec 4, 2019

--

Building out my Module 2 Project at the Flatiron school, my partner and I decided to build out a domain where users can put up a home for sale as a listing. As we know, in Ruby, numbers without decimal points are called integers, and numbers with decimal points are usually called floating-point numbers, or floats. When we began coding our project, most commonly a house object would have an attribute for its price as an integer. When we would display the price of a home in the view files, we wrote our code like this:

Displaying the price of the listing to the view.

And we would see something like this in our view:

** No commas because these numbers are Integers in our database **

Although this technically is displaying our list prices to the user, I knew there must be a way to keep our prices as integers while still being able to have commas separating big numbers so that it can be read easier by the user. As a common practice for all developers, I turned to the web to find a solution.

I discovered that Rails has something called Number Helpers. Rails Number Helpers provides methods for converting numbers into formatted strings. There are methods provided for phone numbers, currency, percentage, precision, positional notation, and file size.

The method I found to be easiest to implement in our program was number_with_delimiter. Now, all I needed to do was pass my “listing.price” as an argument to this built in helper method.

** pic of code with delimiter:

Which would alter our view slightly to:

Definitely easier to read for the user

This was a simple solution to make our application a little bit easier to read for our users, while still being able to maintain integer state for our price attributes. This was incredibly easy to implement. Ruby number helpers provides a few built in methods for converting integers like phone numbers, or currency into better readable strings while still being able to be treated like an integer.

Documentation on Ruby Number Helpers:

https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001971

--

--

Barak Saidoff

A creative Full Stack Developer with an entrepreneurial spirit, proficient at Web Application Development using Git and modern web tools.