Showing posts with label Money calculations java. Show all posts
Showing posts with label Money calculations java. Show all posts

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.

Wednesday, November 27, 2019

Monetary calculations in Java Using float or double !!!

Monetary calculations in Java :

Money plays an important factor in life and a Software engineer has to deal with money calculations like monetary arithmetic a lot during software development. If it is an e-commerce or banking application, then most of the calculations are on money only. But sometimes 0.01 value also makes the whole monetary arithmetic wrong.  That time we trigger our mind button and keeps on debugging, calculations manually bla bla bla !!! But in the end we sometimes weren't able to find the solutions; but actually, the solutions are dancing in-front, but we are not able to catch it.

When you deal with “money” value, always a question arises – Should I use double or float datatype to represent the monetary values?
Answer: Neither double nor float. Oh, boss!!!!! Why and what should I use ??

Why not double/float :

Let's take an example :

double amount1 =2.15;
double amount2 =1.10;
while( (amount1-amount2) !=1.05){
   System.out.println("Oh!!! Stil not true... I am getting stuck infinite lopp...but why...");
}

Output:
Infinite loop. . . .......................
............................

This code will result in an infinite loop because the result of subtraction of amount1 and amount 2 will not be 1.5 instead it would be "1.xxxxxxxxxxxxx" which make boolean condition always true.

System.out.println(2.00-1.1); // check the o/p

System.out.println(42/85); //check the o/p


Solution :

This is one of the common mistakes Java programmers make until they are familiar with BigDecimal class. We can avoid the above mistakes by using BigDecimal class as follows :


BigDecimal amount1 =new BigDecimal("2.15");
BigDecimal amount2 =new BigDecimal("1.10");
while( ! new BigDecimal((amount1.subtract(amount2).equals(new BigDecimal("1.05"))))
   System.out.println("Oh!!!  I am safe now. . .);
}

Output:
The difference of two amount is 1.05 and as the condition evaluates to false, no o/p will come.

Using Incorrect BigDecimal constructor

Another mistake Java Programmers make is using the wrong constructor of BigDecmialBigDecimal has an overloaded constructor and if you use the one which accepts double as an argument you will get the same result as you do while operating with double. So always use BigDecimal with String constructor. here is an example of using BigDecmial constructed with double values:

//Creating BigDecimal from double values
BigDecimal amount3 = new BigDecimal(2.15);
BigDecimal amount4 = new BigDecimal(1.10) ;
System.out.println("difference between 2.15 and 1.0 using BigDecmial is: " + (amount3.subtract(amount4)));

Output:
difference between 2.15 and 1.0 using double is: 1.0499999999999998
difference between 2.15 and 1.0 using BigDecmial is: 1.049999999999999822364316059974953532218933105468750

Conclusion :

  • Don’t use float and double on monetary calculation.
  • Use BigDecimal, long or int for monetary calculation.
  • Use BigDecimal with String constructor and avoid double one.
  • Use Money API (JSR-354

Note :

The performance of BigDecimal operations is slower than the performance of primitive types such as double, float .