Improved format_large_number that rounds to one decimal place but doesn't include tenths place if it's a zero

This commit is contained in:
Victoria Wang 2014-11-09 21:39:40 -06:00
parent b7cc13b9c6
commit 0b22717fc1

View file

@ -22,16 +22,32 @@ class Numeric
end
def format_large_number
if self > 999999999
return sprintf "%.3gB", (self/1000000000.0)
elsif self > 999999
return sprintf "%.3gM", (self/1000000.0)
elsif self > 9999
return sprintf "%.3gK", (self/1000.0)
elsif self > 999
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
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
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
else
return self
if self > 999
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
else
return self
end
end
end