add separator in js for handle showing value is number
This commit is contained in:
@@ -1,24 +1,37 @@
|
||||
export default GlobalConfig = window.GlobalConfig;
|
||||
|
||||
export function addThousandSeparators(value, fractionDigits = 2) {
|
||||
if (!value) return null; // Handle empty or null values
|
||||
if (!value && value !== 0) return null; // Handle empty or null values, but allow 0
|
||||
|
||||
// Remove any non-numeric characters except commas and dots
|
||||
value = value.replace(/[^0-9,.]/g, "");
|
||||
|
||||
// If the value contains multiple dots, assume dots are thousand separators
|
||||
if ((value.match(/\./g) || []).length > 1) {
|
||||
value = value.replace(/\./g, "");
|
||||
// Convert to string first if it's a number
|
||||
if (typeof value === "number") {
|
||||
return new Intl.NumberFormat("en-US", {
|
||||
minimumFractionDigits: fractionDigits,
|
||||
maximumFractionDigits: fractionDigits,
|
||||
}).format(value);
|
||||
}
|
||||
|
||||
// Convert to a proper decimal number
|
||||
let number = parseFloat(value.replace(",", "."));
|
||||
// If it's already a string, process it
|
||||
if (typeof value === "string") {
|
||||
// Remove any non-numeric characters except commas and dots
|
||||
value = value.replace(/[^0-9,.]/g, "");
|
||||
|
||||
if (isNaN(number)) return null; // Return null if conversion fails
|
||||
// If the value contains multiple dots, assume dots are thousand separators
|
||||
if ((value.match(/\./g) || []).length > 1) {
|
||||
value = value.replace(/\./g, "");
|
||||
}
|
||||
|
||||
// Format the number with thousand separators
|
||||
return new Intl.NumberFormat("en-US", {
|
||||
minimumFractionDigits: fractionDigits,
|
||||
maximumFractionDigits: fractionDigits,
|
||||
}).format(number);
|
||||
// Convert to a proper decimal number
|
||||
let number = parseFloat(value.replace(",", "."));
|
||||
|
||||
if (isNaN(number)) return null; // Return null if conversion fails
|
||||
|
||||
// Format the number with thousand separators
|
||||
return new Intl.NumberFormat("en-US", {
|
||||
minimumFractionDigits: fractionDigits,
|
||||
maximumFractionDigits: fractionDigits,
|
||||
}).format(number);
|
||||
}
|
||||
|
||||
return null; // Return null for unsupported types
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user