2014-05-02 17:44:31 -07:00
|
|
|
class Numeric
|
2014-10-15 19:06:39 -07:00
|
|
|
ONE_MEGABYTE = 1000000
|
2014-10-07 02:22:08 +02:00
|
|
|
|
2014-05-02 17:44:31 -07:00
|
|
|
def roundup(nearest=10)
|
|
|
|
self % nearest == 0 ? self : self + nearest - (self % nearest)
|
|
|
|
end
|
2014-10-07 02:22:08 +02:00
|
|
|
|
|
|
|
def to_mb
|
|
|
|
self/ONE_MEGABYTE.to_f
|
|
|
|
end
|
|
|
|
|
2014-10-15 19:06:39 -07:00
|
|
|
def to_bytes_pretty
|
2015-05-06 18:12:24 -07:00
|
|
|
computed = nil
|
|
|
|
unit = nil
|
|
|
|
{
|
|
|
|
'B' => 1000,
|
|
|
|
'KB' => 1000 * 1000,
|
|
|
|
'MB' => 1000 * 1000 * 1000,
|
|
|
|
'GB' => 1000 * 1000 * 1000 * 1000,
|
|
|
|
'TB' => 1000 * 1000 * 1000 * 1000 * 1000
|
|
|
|
}.each_pair { |e, s|
|
|
|
|
if self < s
|
|
|
|
computed = (self.to_f / (s / 1000)).round(2)
|
|
|
|
unit = e
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
computed = computed.to_i if computed.modulo(1) == 0.0
|
|
|
|
|
|
|
|
"#{computed} #{unit}"
|
2014-10-15 19:06:39 -07:00
|
|
|
end
|
|
|
|
|
2017-06-03 18:08:35 -07:00
|
|
|
def to_gigabytes_pretty
|
|
|
|
self.to_gigabytes.to_s + ' GB'
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_gigabytes
|
|
|
|
self / (1000**3)
|
|
|
|
end
|
|
|
|
|
2015-04-06 17:16:15 -07:00
|
|
|
def to_comma_separated
|
2016-05-23 17:23:55 -04:00
|
|
|
self.to_i.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
|
2015-04-06 17:16:15 -07:00
|
|
|
end
|
|
|
|
|
2014-11-09 19:33:17 -06:00
|
|
|
def format_large_number
|
2017-01-09 03:01:14 -06:00
|
|
|
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
|
2014-11-09 21:39:40 -06:00
|
|
|
if self > 9999
|
|
|
|
if self > 999999999
|
|
|
|
unit_char = 'B' #billion
|
|
|
|
unit_amount = 1000000000.0
|
|
|
|
elsif self > 999999
|
|
|
|
unit_char = 'M' #million
|
|
|
|
unit_amount = 1000000.0
|
|
|
|
elsif self > 9999
|
|
|
|
unit_char = 'K' #thousand
|
|
|
|
unit_amount = 1000.0
|
|
|
|
end
|
2015-04-06 17:16:15 -07:00
|
|
|
|
2014-11-09 21:39:40 -06:00
|
|
|
self_divided = self.to_f / unit_amount
|
|
|
|
self_rounded = self_divided.round(1)
|
|
|
|
|
|
|
|
if self_rounded.denominator == 1
|
|
|
|
return sprintf ("%.0f" + unit_char), self_divided
|
|
|
|
else
|
|
|
|
return sprintf ("%.1f" + unit_char), self_divided
|
|
|
|
end
|
2014-11-09 16:42:18 -08:00
|
|
|
else
|
2014-11-09 21:39:40 -06:00
|
|
|
if self > 999
|
|
|
|
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
|
|
|
|
else
|
|
|
|
return self
|
|
|
|
end
|
2014-11-09 16:42:18 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-15 19:06:39 -07:00
|
|
|
def to_space_pretty
|
|
|
|
to_bytes_pretty
|
2014-10-07 02:22:08 +02:00
|
|
|
end
|
2024-02-17 10:27:02 -06:00
|
|
|
|
|
|
|
def not_an_integer?
|
|
|
|
!self.integer?
|
|
|
|
end
|
2015-03-12 13:00:55 -05:00
|
|
|
end
|