Achieve 3-digit delimited display in Ruby

If you want to display a 3-digit delimiter

  1. Use the to_s (: delimited) method
  2. Add require at the beginning of the file to use the above method active_support is a component of Ruby on Rails that allows you to use various methods

num.rb


require "active_support/core_ext/numeric/conversions"

10000.to_s(:delimited)
# => "10,000"

Details can be found on the Active Support page of the Rails Guide (6.3 Formatting) https://railsguides.jp/active_support_core_extensions.html#%E6%9B%B8%E5%BC%8F%E8%A8%AD%E5%AE%9A

Afterword

When I was looking for a 3-digit display method "It seems that the .to_s (: delimited) method can be used to separate numbers into 3 digits!" I didn't know that require was needed and encountered the following error.

in `to_s': no implicit conversion of Symbol into Integer (TypeError)

I can use to_s normally, but when I add (: delimited), I get an error and am in trouble. Therefore, if you use the to_s (: delimited) method with ruby, you must require to enable active_support.

I would like to take this opportunity to learn and utilize other useful methods of active_support.

Recommended Posts

Achieve 3-digit delimited display in Ruby
Class in Ruby
Heavy in Ruby! ??
Display PlantUML in MkDocs
About eval in Ruby
Output triangle in Ruby
Variable type in ruby
Fast popcount in Ruby
[Ruby on Rails] Quickly display the page title in the browser
Display weather forecast using OpenWeatherMap, an external API in Ruby
How to display a graph in Ruby on Rails (LazyHighChart)
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Read design patterns in Ruby
Write class inheritance in Ruby
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
Use ruby variables in javascript.
Multiplication in a Ruby array
About regular expressions in Ruby
Display Flash messages in Rails
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
Find Roman numerals in Ruby
Try using gRPC in Ruby
[Ruby] Find numbers in arrays
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby