Monday, December 16, 2019

Do you know Precision and Scale of a Number ?




You might be thinking what is this stupidity blog, I know it. 
Can you please fill the blank in this table :


Number
Precision
Scale
0.0
?
?
567.234
?
?
-56.3457
6
?
50
?
0


Ok, it is for those who know it but forget.


I was not clear about the Precision and Scale of a Number perfectly !!!
My boss looked at me surprisingly and asked me, is It? Honestly, I told yes.
Then instead of explaining to me, he told me to read it again and explain to him.
I did a wrong explanation knowingly and he agreed. i.e. he has also forgotten the same !!!


So are you with me too? This blog will help to refresh the bean which we have eaten during
our schooling !!!


 What is precision?

Precision refers to the amount of information that is conveyed by a number in terms of its digits.
i.e Precision is the total number of significant digits in a number.


What is Scale?

 - Scale is the number of digits to the right of the decimal point.


So now the above table will be like :


Number
Precision
Scale
0.0
1
1
567.234
6
3
-56.3457
6
4
50
2
0

Where is it Used?
It is used in most of the calculations even in Database too. As a Java developer,
if you are performing any operations using BigDecimal,
you might have seen the use cases such as MathContext  & add method of BigDecimal.


In the MySql database, Decimals can be added by using the DECIMAL(M, D) data type. This requires 2 arguments.
  • M is the maximum number of digits, ranging from 1 to 65.
  • D is the number of decimal places available, ranging from 0 to 30.
Note that with D digits reserved for decimal places, there can be at most M-D digits available for the integer part. So if we use DECIMAL(6,4), the number 45.8239 would be valid, but 456.12 would not.