Para representar valores monetarios se recomienda usar:
Para decidir cuál de los tres usar, es importante tener en cuenta el número de dígitos que manejará la aplicación (más o menos).
Si número de dígitos:
<=9
: usar BigDecimal
, int
, o long
<=18
: usar BigDecimal
o long
>18
: usar solamente BigDecimal
Cabe señalar que la clase Currency
encapsula identificadores estándar para las monedas del mundo.
Para representar valores monetarios, no se recomienda usar:
porque siempre llevan pequeñas diferencias de redondeo.
Algunas cosas a tomar en cuenta con respecto a BigDecimal
:
El constructor recomendado es BigDecimal (String)
, no BigDecimal (double)
- ver javadoc (Cuando se usa el constructor double
los resultados pueden ser impredecibles).
Los objetos BigDecimal
son inmutables: las operaciones siempre devuelven objetos nuevos y nunca modifican el estado de los objetos existentes
El estilo ROUND_HALF_EVEN
de redondeo introduce el sesgo mínimo. También se llama el redondeo de los banqueros, o redondo-a-igualar.
Ejemplo de código:
He aquí un ejemplo de código de cálculos monetarios usando BigDecimal
. El ejemplo está tomado tal cual de su fuente. Realiza operaciones básicas: +, - , * , /
entre dos valores.
import java.math.BigDecimal;
import java.util.Currency;
/**
* Example of typical calculations with monetary values, implemented with
* <tt>BigDecimal</tt>.
*
* <P>This example is for a currency which has two decimal places.
*
* See
* http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
*
* Note in particular how the <em>default</em> scale of the result of an
* operation is calculated from the scales of the two input numbers :
* <ul>
* <li> a + b : max[ scale(a), scale(b) ]
* <li> a - b : max[ scale(a), scale(b) ]
* <li> a * b : scale(a) + scale(b)
* <li> a / b : scale(a) - scale(b)
* </ul>
*/
public final class MoneyCalculation {
/**
* Simple test harness.
*
* Takes two numeric arguments, representing monetary values, in a form
* which can be passed successfully to the <tt>BigDecimal(String)</tt>
* constructor (<tt>25.00, 25.0, 25</tt>, etc).
*
* Note that the <tt>String</tt> constructor is preferred for
* <tt>BigDecimal</tt>.
*/
public static void main(String... aArgs){
BigDecimal amountOne = new BigDecimal(aArgs[0]);
BigDecimal amountTwo = new BigDecimal(aArgs[1]);
MoneyCalculation calc = new MoneyCalculation(amountOne, amountTwo);
calc.doCalculations();
}
public MoneyCalculation(BigDecimal aAmountOne, BigDecimal aAmountTwo){
fAmountOne = rounded(aAmountOne);
fAmountTwo = rounded(aAmountTwo);
}
public void doCalculations() {
log("Amount One: " + fAmountOne);
log("Amount Two: " + fAmountTwo);
log("Sum : " + getSum());
log("Difference : " + getDifference());
log("Average : " + getAverage());
log("5.25% of Amount One: " + getPercentage());
log("Percent Change From Amount One to Two: " + getPercentageChange());
}
// PRIVATE
private BigDecimal fAmountOne;
private BigDecimal fAmountTwo;
/**
* Defined centrally, to allow for easy changes to the rounding mode.
*/
private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;
/**
* Number of decimals to retain. Also referred to as "scale".
*/
private static int DECIMALS = 2;
//An alternate style for this value :
//private static int DECIMAL_PLACES =
// Currency.getInstance("USD").getDefaultFractionDigits()
//;
private static int EXTRA_DECIMALS = 4;
private static final BigDecimal TWO = new BigDecimal("2");
private static BigDecimal HUNDRED = new BigDecimal("100");
private static BigDecimal PERCENTAGE = new BigDecimal("5.25");
private void log(String aText){
System.out.println(aText);
}
private BigDecimal getSum(){
return fAmountOne.add(fAmountTwo);
}
private BigDecimal getDifference(){
return fAmountTwo.subtract(fAmountOne);
}
private BigDecimal getAverage(){
return getSum().divide(TWO, ROUNDING_MODE);
}
private BigDecimal getPercentage(){
BigDecimal result = fAmountOne.multiply(PERCENTAGE);
result = result.divide(HUNDRED, ROUNDING_MODE);
return rounded(result);
}
private BigDecimal getPercentageChange(){
BigDecimal fractionalChange = getDifference().divide(
fAmountOne, EXTRA_DECIMALS, ROUNDING_MODE
);
return rounded(fractionalChange.multiply(HUNDRED));
}
private BigDecimal rounded(BigDecimal aNumber){
return aNumber.setScale(DECIMALS, ROUNDING_MODE);
}
}
Para más detalles: Java Best Practices: Representing money
Una clase Money
Durante años la comunidad de programadores echaba de menos una clase Money
que ayudase a manejar situaciones como estas.
Finalmente, el JDK 9 trae incorporada la API Money en el JSR 354, para manejar modelos de dinero y de monedas.
Ver: