We need BigDecimal for financial Calculations. For example:
double a = 0.02;
double b = 0.03;
double c = b - a;
System.out.println(c);
BigDecimal _a = new BigDecimal("0.02");
BigDecimal _b = new BigDecimal("0.03");
BigDecimal _c = _b.subtract(_a);
System.out.println(_c);
Program output:
0.009999999999999998
0.01
Unfortunately, scala does not support bigdecimal literal. Sometimes it is very inconveniently, for example in pattern matching, we have to convert value to string.
//Nn, NNull are extractor objects
val vals = None.nn::10.nn::20.nn::"20.5000".nn::"20.5".nn::Nil
for(nCode <- vals ) {
nCode match {
case NNull() =>
println("null")
case Nn("20.5") =>
println("20.5")
case x =>
println(s"else: ${x}")
}
}
It is inconveniently, and there is performance loss.
Is there any plan to add bigdecimal literal in scala?
For example:
20b ā scala.math.Bigdecimal
With such literal it will be possible to write:
nCode.get match {
case null =>
println("null")
case 20.5b =>
println(20.5b)
case x =>
println(s"else: ${x}")
}
To say the truth when we write erp modules, there is only one question: Why has not it been done yet? :))
For example in oracle sql (https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#i139891)
Select 4.5 from dual
Return decimal.