|
|
@ -5,43 +5,6 @@ import { IObjectParam, IParam } from '../../interfaces/objects' |
|
|
|
import TCBParameter from './TCBParameter' |
|
|
|
import TableValue from './TableValue' |
|
|
|
|
|
|
|
export function formatNumericValue(format: string, value: string) { |
|
|
|
// Extract precision and scale from the format string
|
|
|
|
const regex = /numeric\((\d+),(\d+)\)/; |
|
|
|
const match = format.match(regex); |
|
|
|
if (!match) return value; // return original if format is not correct
|
|
|
|
|
|
|
|
const precision = parseInt(match[1], 10); // Total number of digits
|
|
|
|
const scale = parseInt(match[2], 10); // Number of digits after the decimal point
|
|
|
|
|
|
|
|
// Convert value to a number and handle cases like empty value or invalid input
|
|
|
|
const numericValue = parseFloat(value); |
|
|
|
|
|
|
|
if (isNaN(numericValue)) { |
|
|
|
return '0'.padStart(precision - scale, '0') + '.' + '0'.repeat(scale); // fallback in case of invalid value
|
|
|
|
} |
|
|
|
|
|
|
|
// Ensure the value has the correct number of digits after the decimal point (scale)
|
|
|
|
let formattedValue = numericValue.toFixed(scale); |
|
|
|
|
|
|
|
// Ensure the total length doesn't exceed the precision
|
|
|
|
const totalDigits = formattedValue.replace('.', '').length; |
|
|
|
|
|
|
|
if (totalDigits > precision) { |
|
|
|
// Truncate the value if it exceeds the total precision
|
|
|
|
formattedValue = numericValue.toPrecision(precision); |
|
|
|
} |
|
|
|
|
|
|
|
// Pad with leading zeros if necessary (if it's an integer and the precision is greater than the scale)
|
|
|
|
const [integerPart, decimalPart] = formattedValue.split('.'); |
|
|
|
|
|
|
|
// Ensure the integer part doesn't exceed the precision
|
|
|
|
const paddedInteger = integerPart.padStart(precision - scale, '0'); |
|
|
|
|
|
|
|
// Reassemble the number
|
|
|
|
return `${paddedInteger}.${decimalPart.padEnd(scale, '0')}`; |
|
|
|
} |
|
|
|
|
|
|
|
interface ObjectParameterProps { |
|
|
|
showLabel?: boolean, |
|
|
|
param: IObjectParam, |
|
|
|