8

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.

3
  • 1
    postgresql.org/docs/current/static/… Commented Oct 16, 2014 at 5:35
  • @CraigRinger, thanks. What I understood from this is max value can be of 10^10. i.e. 9999999999.9999999999 Commented Oct 16, 2014 at 6:13
  • ((10^20)-1)/(10^10) actually. Because of the fractional part. Commented Oct 16, 2014 at 7:03

2 Answers 2

17

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 .

Details in the manual.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.