Module: BlackStack::Number::Encoding

Defined in:
lib/functions.rb

Overview


Encoding


Class Method Summary collapse

Class Method Details

.encode_minutes(n) ⇒ Object

Convierte una cantidad de minutos a una leyenda legible por el usuario. Ejemplo: “2 days, 5 hours” Ejemplo: “4 hours, 30 minutes” Ejemplo: “3 days, 4 hour”



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/functions.rb', line 129

def self.encode_minutes(n)
  # TODO: validar que n sea un entero mayor a 0      
  if (n<0)
    return "?"
  end
  if (n<60)
    return "#{n} minutes"
  elsif (n<24*60)
    return "#{(n/60).to_i} hours, #{n-60*(n/60).to_i} minutes"
  else
    return "#{(n/(24*60)).to_i} days, #{((n-24*60*(n/(24*60)).to_i)/60).to_i} hours"
  end
end

.format_with_separator(number) ⇒ Object

Converts number to a string with a format like xx,xxx,xxx.xxxx number: it may be int or float



120
121
122
123
# File 'lib/functions.rb', line 120

def self.format_with_separator(number)
  whole_part, decimal_part = number.to_s.split('.')
  [whole_part.gsub(/(\d)(?=\d{3}+$)/, '\1,'), decimal_part].compact.join('.')
end