Skip to content

Added a tip - Convert number into human readable format #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Being impressed with the awesomeness of jstip repository, I came up with the ide
Please feel free to send us a pull request with your Rails tip to be published here. Any improvements or suggestions are more than welcome!

# Tips list
- 29 - [Convert a number into human readable](https://github.com/JPMallow/rails_tips/blob/master/rails_tip/2016-07-20-human_readable_number.md)
- 28 - [Support for left outer join in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-10-left_outer_join_in_Rails_5.md)
- 27 - [Render partial from cache faster](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-09-rendering_partial_from_cache_faster.md)
- 26 - [Increase productivity with console tricks](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-08-Increase_productivity_with_few_tricks.md)
Expand Down
52 changes: 52 additions & 0 deletions rails_tip/2016-07-20-human_readable_number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Convert a number into human readable
tip-number: 29
tip-username: Jayaprakash
tip-username-profile: https://github.com/JPMallow
tip-description: To convert the number into human readable format, you can use number_to_human helper method in Rails - http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

---

To convert the integer numbers into human readable format.

```ruby
number_to_human(123) # => "123"
number_to_human(1234) # => "1.23 Thousand"
number_to_human(12345) # => "12.3 Thousand"
number_to_human(1234567) # => "1.23 Million"
number_to_human(1234567890) # => "1.23 Billion"
number_to_human(1234567890123) # => "1.23 Trillion"
number_to_human(1234567890123456) # => "1.23 Quadrillion"
number_to_human(1234567890123456789) # => "1230 Quadrillion"
number_to_human(489939, precision: 2) # => "490 Thousand"
number_to_human(489939, precision: 4) # => "489.9 Thousand"
number_to_human(1234567, precision: 4,
significant: false) # => "1.2346 Million"
number_to_human(1234567, precision: 1,
separator: ',',
significant: false) # => "1,2 Million"

number_to_human(500000000, precision: 5) # => "500 Million"
number_to_human(12345012345, significant: false) # => "12.345 Billion"
```

Non-significant zeros after the decimal separator are stripped out by default (set :strip_insignificant_zeros to false to change that):

```ruby
number_to_human(12.0000) # => “12”
number_to_human(12.0000, precision: 4, strip_insignificant_zeros: false) # => “12.00”
```

### Troubleshooting

If you receive the following error

```
NoMethodError: undefined method `number_to_human' for main:Object
```

include NumberHelper in your controller / views.

```ruby
include ActionView::Helpers::NumberHelper
```