Format numbers larger than 100m

pull/859/head
Uku Taht 6 years ago
parent 3eb2f6d7a8
commit fb64345e92

@ -2,6 +2,7 @@ const THOUSAND = 1000
const HUNDRED_THOUSAND = 100000
const MILLION = 1000000
const HUNDRED_MILLION = 100000000
const BILLION = 1000000000
export default function numberFormatter(num) {
if (num >= THOUSAND && num < MILLION) {
@ -11,9 +12,9 @@ export default function numberFormatter(num) {
} else {
return (Math.floor(thousands * 10) / 10) + 'k'
}
} else if (num >= MILLION && num < HUNDRED_MILLION) {
} else if (num >= MILLION && num < BILLION) {
const millions = num / MILLION
if (millions === Math.floor(millions)) {
if (millions === Math.floor(millions) || num >= HUNDRED_MILLION) {
return Math.floor(millions) + 'M'
} else {
return (Math.floor(millions * 10) / 10) + 'M'

@ -27,7 +27,7 @@ defmodule PlausibleWeb.StatsView do
n >= 1_000_000 && n < 1_000_000_000 ->
millions = trunc(n / 100_000) / 10
if millions == trunc(millions) do
if millions == trunc(millions) || n > 100_000_000 do
"#{trunc(millions)}m"
else
"#{millions}m"

@ -42,5 +42,9 @@ defmodule PlausibleWeb.StatsView.Test do
test "99_999_999 becomes 99.9m" do
assert StatsView.large_number_format(99_999_999) == "99.9m"
end
test "101_000_000 becomes 101m" do
assert StatsView.large_number_format(101_000_000) == "101m"
end
end
end

Loading…
Cancel
Save