In one of my PostgreSQL table there is an attribute result with datatype Numeric(20,10).
One of its input value can be +/- infinity.
I want to know with the length (20,10) what are the maximum & minimum values.
For a numeric of numeric(precision, scale), the limit values are:
+- ((10^precision)-1)/(10^scale)
so for numeric(20,10) that'd be ((10^20)-1)/(10^10) or 9999999999.9999999999 .
For long decimals, you might run into precision errors in languages like JavaScript. So you can do something like this.
const calcluateDecimalMax = (x: number, y: number): string => {
const leftCount = x - y
const rightCount = y
const leftString = "9".repeat(leftCount)
const rightString = "9".repeat(rightCount)
return `${leftString}.${rightString}`
}
console.log(calcluateDecimalMax(25, 9)) // "9999999999999999.999999999"
((10^20)-1)/(10^10)actually. Because of the fractional part.