Skip to main content
added 9 characters in body
Source Link
phuclv
  • 43.2k
  • 16
  • 191
  • 556

Ruby already has a built in solution:

  
0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

In Rails, ActiveRecord numerical attributes can be converted too:

product.size = 0.33 product.size.to_r.to_s # => "33/100" 
product.size = 0.33 product.size.to_r.to_s # => "33/100" 

Ruby already has a built in solution:

 
0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

In Rails, ActiveRecord numerical attributes can be converted too:

product.size = 0.33 product.size.to_r.to_s # => "33/100" 

Ruby already has a built in solution:

 
0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

In Rails, ActiveRecord numerical attributes can be converted too:

product.size = 0.33 product.size.to_r.to_s # => "33/100" 
Added Rails example
Source Link
Josh W Lewis
  • 1.3k
  • 11
  • 19

Ruby already has a built in solution:

0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

In Rails, ActiveRecord numerical attributes can be converted too:

product.size = 0.33 product.size.to_r.to_s # => "33/100" 

Ruby already has a built in solution:

0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

Ruby already has a built in solution:

0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5" 

In Rails, ActiveRecord numerical attributes can be converted too:

product.size = 0.33 product.size.to_r.to_s # => "33/100" 
Source Link
Josh W Lewis
  • 1.3k
  • 11
  • 19

Ruby already has a built in solution:

0.33.rationalize.to_s # => "33/100" 0.4.rationalize.to_s # => "2/5"