fix: Unsatisfactory rounding of download sums (#3872)
Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com>
This commit is contained in:
parent
e5030a8fbe
commit
dd98a1316a
@ -1,18 +1,26 @@
|
|||||||
import { createFormatter, type Formatter } from "@vintl/compact-number";
|
const formatters = new WeakMap<object, Intl.NumberFormat>();
|
||||||
import type { IntlController } from "@vintl/vintl/controller";
|
|
||||||
|
|
||||||
const formatters = new WeakMap<IntlController<any>, Formatter>();
|
export function useCompactNumber(truncate = false, fractionDigits = 2, locale?: string) {
|
||||||
|
const context = {};
|
||||||
|
|
||||||
export function useCompactNumber(): Formatter {
|
let formatter = formatters.get(context);
|
||||||
const vintl = useVIntl();
|
|
||||||
|
|
||||||
let formatter = formatters.get(vintl);
|
if (!formatter) {
|
||||||
|
formatter = new Intl.NumberFormat(locale, {
|
||||||
if (formatter == null) {
|
notation: "compact",
|
||||||
const formatterRef = computed(() => createFormatter(vintl.intl));
|
maximumFractionDigits: fractionDigits,
|
||||||
formatter = (value, options) => formatterRef.value(value, options);
|
});
|
||||||
formatters.set(vintl, formatter);
|
formatters.set(context, formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatter;
|
function format(value: number): string {
|
||||||
|
let formattedValue = value;
|
||||||
|
if (truncate) {
|
||||||
|
const scale = Math.pow(10, fractionDigits);
|
||||||
|
formattedValue = Math.floor(value * scale) / scale;
|
||||||
|
}
|
||||||
|
return formatter!.format(formattedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return format;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -98,7 +98,10 @@
|
|||||||
{{ formatCompactNumber(projects?.length || 0) }}
|
{{ formatCompactNumber(projects?.length || 0) }}
|
||||||
projects
|
projects
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-2 font-semibold">
|
<div
|
||||||
|
v-tooltip="sumDownloads.toLocaleString()"
|
||||||
|
class="flex items-center gap-2 font-semibold"
|
||||||
|
>
|
||||||
<DownloadIcon class="h-6 w-6 text-secondary" />
|
<DownloadIcon class="h-6 w-6 text-secondary" />
|
||||||
{{ formatCompactNumber(sumDownloads) }}
|
{{ formatCompactNumber(sumDownloads) }}
|
||||||
downloads
|
downloads
|
||||||
@ -282,7 +285,7 @@ import NavTabs from "~/components/ui/NavTabs.vue";
|
|||||||
const vintl = useVIntl();
|
const vintl = useVIntl();
|
||||||
const { formatMessage } = vintl;
|
const { formatMessage } = vintl;
|
||||||
|
|
||||||
const formatCompactNumber = useCompactNumber();
|
const formatCompactNumber = useCompactNumber(true);
|
||||||
|
|
||||||
const auth = await useAuth();
|
const auth = await useAuth();
|
||||||
const user = await useUser();
|
const user = await useUser();
|
||||||
|
|||||||
@ -80,6 +80,7 @@
|
|||||||
projects
|
projects
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
|
v-tooltip="sumDownloads.toLocaleString()"
|
||||||
class="flex items-center gap-2 border-0 border-r border-solid border-divider pr-4 font-semibold"
|
class="flex items-center gap-2 border-0 border-r border-solid border-divider pr-4 font-semibold"
|
||||||
>
|
>
|
||||||
<DownloadIcon class="h-6 w-6 text-secondary" />
|
<DownloadIcon class="h-6 w-6 text-secondary" />
|
||||||
@ -389,7 +390,7 @@ const config = useRuntimeConfig();
|
|||||||
const vintl = useVIntl();
|
const vintl = useVIntl();
|
||||||
const { formatMessage } = vintl;
|
const { formatMessage } = vintl;
|
||||||
|
|
||||||
const formatCompactNumber = useCompactNumber();
|
const formatCompactNumber = useCompactNumber(true);
|
||||||
|
|
||||||
const formatRelativeTime = useRelativeTime();
|
const formatRelativeTime = useRelativeTime();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user