Module: UnitConversions
- Defined in:
- lib/unit_conversions.rb
Overview
Pure-function helpers for converting between inches and feet/inches
notation (72 → 6′) and approximating decimal numbers as Rationals
with a bounded denominator. Used by the design-tool / room-layout
views where measurements need both decimal and "feet-inches" formats.
Class Method Summary collapse
- .closest_rational(number, max_denominator = 1000) ⇒ Object
- .inches_to_feetinches(inches, separator: ' ') ⇒ Object
Class Method Details
.closest_rational(number, max_denominator = 1000) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/unit_conversions.rb', line 25 def self.closest_rational(number, max_denominator = 1000) return if number.blank? # Convert the number to a rational approximation with a limited denominator Rational(number).rationalize(Rational(1, max_denominator)) end |
.inches_to_feetinches(inches, separator: ' ') ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/unit_conversions.rb', line 7 def self.inches_to_feetinches(inches, separator: ' ') return nil if inches.nil? inches = inches.to_f s = [] ft = (inches / 12).truncate inches = (inches % 12) if ft.positive? ft = ActionController::Base.helpers.number_with_precision ft, precision: 0, strip_insignificant_zeros: true s << "#{ft}′" end if inches.positive? inches = ActionController::Base.helpers.number_with_precision inches, precision: 3, strip_insignificant_zeros: true s << "#{inches}″" end s.join(separator) end |