mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
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:
parent
b7cc13b9c6
commit
0b22717fc1
1 changed files with 25 additions and 9 deletions
|
@ -22,18 +22,34 @@ class Numeric
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_large_number
|
def format_large_number
|
||||||
|
if self > 9999
|
||||||
if self > 999999999
|
if self > 999999999
|
||||||
return sprintf "%.3gB", (self/1000000000.0)
|
unit_char = 'B' #billion
|
||||||
|
unit_amount = 1000000000.0
|
||||||
elsif self > 999999
|
elsif self > 999999
|
||||||
return sprintf "%.3gM", (self/1000000.0)
|
unit_char = 'M' #million
|
||||||
|
unit_amount = 1000000.0
|
||||||
elsif self > 9999
|
elsif self > 9999
|
||||||
return sprintf "%.3gK", (self/1000.0)
|
unit_char = 'K' #thousand
|
||||||
elsif self > 999
|
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
|
||||||
|
if self > 999
|
||||||
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
|
return self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
|
||||||
else
|
else
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_space_pretty
|
def to_space_pretty
|
||||||
to_bytes_pretty
|
to_bytes_pretty
|
||||||
|
|
Loading…
Add table
Reference in a new issue