fix(roundNumber): handle negative numbers properly (#4336)
This commit is contained in:
parent
a2be64f899
commit
7377db8d28
@ -10,22 +10,31 @@
|
|||||||
* @param value - to return a fixed measurement value from
|
* @param value - to return a fixed measurement value from
|
||||||
* @param precision - defining how many digits after 1..9 are desired
|
* @param precision - defining how many digits after 1..9 are desired
|
||||||
*/
|
*/
|
||||||
function roundNumber(value: string | number, precision = 2): string {
|
|
||||||
if (value === undefined || value === null || value === '') return 'NaN';
|
function roundNumber(value, precision = 2) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return value.map((v) => roundNumber(v, precision)).join(", ");
|
||||||
|
}
|
||||||
|
if (value === undefined || value === null || value === "") {
|
||||||
|
return "NaN";
|
||||||
|
}
|
||||||
value = Number(value);
|
value = Number(value);
|
||||||
if (value < 0.0001) return `${value}`;
|
const absValue = Math.abs(value);
|
||||||
|
if (absValue < 0.0001) {
|
||||||
|
return `${value}`;
|
||||||
|
}
|
||||||
const fixedPrecision =
|
const fixedPrecision =
|
||||||
value >= 100
|
absValue >= 100
|
||||||
? precision - 2
|
? precision - 2
|
||||||
: value >= 10
|
: absValue >= 10
|
||||||
? precision - 1
|
? precision - 1
|
||||||
: value >= 1
|
: absValue >= 1
|
||||||
? precision
|
? precision
|
||||||
: value >= 0.1
|
: absValue >= 0.1
|
||||||
? precision + 1
|
? precision + 1
|
||||||
: value >= 0.01
|
: absValue >= 0.01
|
||||||
? precision + 2
|
? precision + 2
|
||||||
: value >= 0.001
|
: absValue >= 0.001
|
||||||
? precision + 3
|
? precision + 3
|
||||||
: precision + 4;
|
: precision + 4;
|
||||||
return value.toFixed(fixedPrecision);
|
return value.toFixed(fixedPrecision);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user