export type valueType = string | number;
export const separator = (
SeparatorString: string,
groupSeparator: string,
): string => {
const res = SeparatorString.replace(/\d+/, function (n) {
return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
return $1 + `${groupSeparator}`;
});
});
return res;
};
export const isHasDot = (value: number): boolean => {
if (!isNaN(value)) {
return (value + '').indexOf('.') !== -1;
}
return false;
};
export const analysisValueType = (
value: valueType,
propsValue: valueType,
groupSeparator: string,
splitPrecisionNumber: number
): string => {
const fixedNumber =
propsValue?.toString().indexOf('.') !== -1
? propsValue?.toString().length - propsValue?.toString().indexOf('.') - 1
: 0;
if (typeof value === 'number') {
if (isHasDot(value)) {
return splitPrecisionNumber
? separator(value.toFixed(splitPrecisionNumber).toString(), groupSeparator)
: separator(value.toFixed(fixedNumber).toString(), groupSeparator);
} else {
return splitPrecisionNumber
? separator(value.toFixed(splitPrecisionNumber).toString(), groupSeparator)
: separator(value.toString(), groupSeparator);
}
} else {
return value;
}
};