Files
lucia-frontend/src/module/shortScaleNumber.js
2026-03-06 18:57:58 +08:00

30 lines
930 B
JavaScript

// The Lucia project.
// Copyright 2023-2026 DSP, inc. All rights reserved.
// Authors:
// chiayin.kuo@dsp.im (chiayin), 2023/1/31
// imacat.yang@dsp.im (imacat), 2023/9/23
// cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/** @module shortScaleNumber Short-scale number formatting. */
/**
* Converts a number to a short-scale abbreviated string with a suffix
* (k for thousands, m for millions, b for billions, t for trillions).
*
* Values are rounded up to one decimal place.
*
* @param {number} number - The number to abbreviate.
* @returns {string} The abbreviated string (e.g. "1.5k ", "2.3m ").
*/
export default function shortScaleNumber(number) {
const abbreviations = ["", "k", "m", "b", "t"];
let index = 0;
let num = number;
while (num >= 1000 && index < abbreviations.length - 1) {
num /= 1000;
index++;
}
num = Math.ceil(num * 10) / 10;
return num + abbreviations[index] + " " ;
}