BigDecimal gets rounded ... but I don't want rounding
In my code I want to insert into db without any rounding or exponentiation .. but it gets converted in my java code when I use sop as 2.631578947368421E-7
Below is the code I am using:
BigDecimal a =new BigDecimal(0.0000002631578947368421052631578947368421052632,MathContext.DECIMAL64);
System.out.println(a);
I just want to be supported because I want to do some calculations.
Please provide me with a suitable solution.
source to share
Construct big decimal with String , otherwise the constant you enter is rounded before being passed as an argument to the constructor BigDecimal
.
So write something like this:
BigDecimal a =new BigDecimal("0.0000002631578947368421052631578947368421052632");
System.out.println(a.toPlainString());
And everything should work.
EDIT: You should also get rid of the second constructor argument as that
static MathContext.DECIMAL64
means:A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.
EDIT2: Also use a.toPlainString()
when printing to avoid using scientific notation.
source to share
BIgDecimal is rounded.
No, it is not. BigDecimal
has nothing to do with it. The constant value is 0.0000002631578947368421052631578947368421052632
rounded. It cannot be represented exactly as a floating point. If you want to get an exact BigDecimal with this value usenew BigDecimal(String).
source to share
You can use MathContext.UNLIMITED.
BigDecimal a =new BigDecimal(0.0000002631578947368421052631578947368421052632,MathContext.UNLIMITED);
System.out.println(a);
System.out.println(a.toPlainString());
When printing a value, you can use BigDecimal.toPlainString () to return a "string representation of this BigDecimal without an exponent field".
source to share