Fixed wonky trailing zero removal

Resolves #623
This commit is contained in:
Donkie
2025-03-18 20:59:57 +01:00
parent a94163dc1a
commit a775beb5bf

View File

@@ -117,10 +117,10 @@ export function enrichText(text: string | undefined) {
*/
export function formatWeight(weightInGrams: number, precision: number = 2): string {
if (weightInGrams >= 1000) {
const kilograms = (weightInGrams / 1000).toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros
const kilograms = removeTrailingZeros((weightInGrams / 1000).toFixed(precision))
return `${kilograms} kg`;
} else {
const grams = weightInGrams.toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros
const grams = removeTrailingZeros(weightInGrams.toFixed(precision))
return `${grams} g`;
}
}
@@ -134,9 +134,30 @@ export function formatWeight(weightInGrams: number, precision: number = 2): stri
*/
export function formatLength(lengthInMillimeter: number, precision: number = 2): string {
if (lengthInMillimeter >= 1000) {
const meters = (lengthInMillimeter / 1000).toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros
const meters = removeTrailingZeros((lengthInMillimeter / 1000).toFixed(precision))
return `${meters} m`;
} else {
return `${lengthInMillimeter} mm`;
}
}
/**
* Removes trailing zeros from a numeric string, including unnecessary decimal points.
*
* This function takes a string representation of a number and removes any trailing zeros
* after the decimal point. If the number ends with a decimal point followed by only zeros,
* the entire decimal portion is removed.
*
* @param num - The numeric string to process.
* @returns The numeric string with trailing zeros removed.
*
* @example
* ```typescript
* removeTrailingZeros("123.45000"); // Returns "123.45"
* removeTrailingZeros("100.000"); // Returns "100"
* removeTrailingZeros("0.0000"); // Returns "0"
* ```
*/
function removeTrailingZeros(num: string): string {
return num.replace(/(\.\d*?[1-9])0+|\.0*$/, '$1');
}