public class MoneyUtils extends Object
| Constructor and Description |
|---|
MoneyUtils() |
| Modifier and Type | Method and Description |
|---|---|
static BigDecimal[] |
allocate(BigDecimal amountInRupees,
long[] weights)
This variant of the allocate method allows the original amount to be
input as a rupee amount i.e.
|
static long[] |
allocate(long amountInPaise,
long[] weights)
Splits up an amount into multiple amounts according to the specified
weightage.
|
static boolean |
isInteger(String str) |
static long |
roundAndConvertToPaise(BigDecimal rupees)
Converts a rupee amount in BigDecimal to a paise amount as an integer
(long).
|
static BigDecimal |
roundOff(BigDecimal amount)
This method is a utility method, which takes a BigDecimal and rounds that
to 0 places.
|
static BigDecimal |
roundOffTwo(BigDecimal amount)
Round off to two decimal big decimal value.
|
public static long[] allocate(long amountInPaise,
long[] weights)
E.g. An amount of 100 and weights (3, 7) will give (30, 70).
The algorithm used is the one described at http://nida.se/patterns/poeaa/money.html. It guarantees that the sum of the splits will exactly equal the input amount, even for fractional cases:
E.g. An amount of 100 and weights (3, 8) will give (28, 72).
In such cases, the individual split amounts might not exactly match the number you'd get from floating-point calculation (27.272 and 72.727 in the example above). With the input amount in paise, the individual split amounts will be at most 1 paisa off.
It is the job of the caller to do the "massaging" of rupee amounts into paise amounts, and vice-versa for the output.
See the unit tests for more examples.
amountInPaise - Has to be whole number, so if your amount is Rs. 100.342,
you'll have to convert that to 10034 paise.weights - Numbers that specify the weight given to each split. They need
not add up to 100% (e.g. 3, 3, 4) - any arbitrary weights will
do. Also note that (2, 10, 16) is identical to (1, 5, 8).public static BigDecimal[] allocate(BigDecimal amountInRupees, long[] weights)
amountInRupees - weights - public static long roundAndConvertToPaise(BigDecimal rupees)
rupees - public static BigDecimal roundOffTwo(BigDecimal amount)
amount - the amountpublic static BigDecimal roundOff(BigDecimal amount)
amount - the amountpublic static boolean isInteger(String str)
Copyright © 2015–2016 eGovernments Foundation. All rights reserved.