Does Java have a BigDecimal set abbreviation?
This is for float. What is for BigDecimal?
import java.math.BigDecimal;
data.setPaymentAmount(25F);
Does Java have a BigDecimal set abbreviation?
This is for float. What is for BigDecimal?
import java.math.BigDecimal;
data.setPaymentAmount(25F);
Replace your float
numeric literal with passing text to a constructor.
data.setPaymentAmount( new BigDecimal( "25" ) );
The first word in the Javadoc for BigDecimal
is “immutable”. So you cannot change the number content of such an object. Calling methods such as add
, subtract
, multiply
, and divide
result in a new separate BigDecimal
object being instantiated.
Never use a float
/Float
or double
/Double
as seen in your code, not if you care about accuracy. Those floating point types trade away accuracy for speed of performance. The BigDecimal
class does the opposite, trading away speed for accuracy.
Your example code uses a float
literal: 25F
. Passing a float
to the constructor of BigDecimal
defeats the purpose of using a BigDecimal
if your purpose is accuracy.
To maintain accuracy, use String
inputs.
BigDecimal x ; // Null object reference. No number involved.
x = new BigDecimal( "25" ) ; // Representing *exactly* twenty-five in a new object of type `BigDecimal`.
In your code, if your set…
method is meant to install a BigDecimal
object, pass a BigDecimal
object.
data.setPaymentAmount( new BigDecimal( "25" ) ) ;