# Java Notes: Monetary Problems

# Java Notes: Monetary Problems

### Looking for exact monetary calculations? Don't forget to use BigDecimal unless you want some nonsensical answers.

Join the DZone community and get the full member experience.

Join For FreeGet the Edge with a Professional Java IDE. 30-day free trial.

Let's try to solve a simple problem we used to solve in elementary school with Java.

The classical problem is:

How many candies, priced at ¢10 , can you buy from market, with $1 in your pocket.?

Our Java Code:

```
public static void main(String[] args) {
float yourMoney = 1.00f;
float priceOfCandy = 0.10f;
int candiesYouBuy = 0;
System.out.println("You have $" + yourMoney + " in your pocket.");
System.out.println("Candies are priced at $" + priceOfCandy);
while (yourMoney >= priceOfCandy) {
candiesYouBuy++;
yourMoney -= priceOfCandy;
}
System.out.println("According to our calculation, you can buy " + candiesYouBuy + " candies and you have $" +
yourMoney + " left in your pocket.");
}
```

__Output:__

*You have $1.0 in your pocket.*

*Candies are priced at $0.1*

*According to our calculation, you can buy 9 candies and you have $0.09999993 left in your pocket.*

What?!

The reason, which we can't see, what we expect is due to the fact that we use float data type for our monetary values. The float and double types are designed primarily for scientific and engineering calculations. They do not, however, provide exact results and should not be used for monetary calculations where exact result are required. We should use BigDecimal, int or long types alternatively.

Let's write our above code again using BigDecimal instead of float :

```
public static void main(String[] args) {
BigDecimal yourMoney = new BigDecimal("1.00");
BigDecimal priceOfCandy = new BigDecimal("0.10");
int candiesYouBuy = 0;
System.out.println("You have $" + yourMoney + " in your pocket.");
System.out.println("Candies are priced at $" + priceOfCandy);
while (yourMoney.compareTo(priceOfCandy) >= 0) {
candiesYouBuy++;
yourMoney = yourMoney.subtract(priceOfCandy);
}
System.out.println("According to our calculation, you can buy " + candiesYouBuy + " candies and you have $" +
yourMoney + " left in your pocket.");
}
```

__Output:__

*You have $1.0 in your pocket.*

*Candies are priced at $0.1*

*According to our calculation, you can buy 10 candies and you have $0.0 left in your pocket.*

Yes, it is ok, now!

Here, we can mention two disadvantages associated with the use of BigDecimal:

1. Less convenient than using primitive types

2. Slower

For simple calculations, the second disadvantage maybe ok , but the first may be annoying.

We can use int or long instead of BigDecimal, alternatively. In this case, we have to keep track of the decimal point, depending on the amounts involved. So, in the above code, we have to do our calculations in cents instead of dollars:

```
public static void main(String[] args) {
int yourMoney = 100;
int priceOfCandy = 10;
int candiesYouBuy = 0;
System.out.println("You have $" + (yourMoney*0.01) + " in your pocket.");
System.out.println("Candies are priced at $" + (priceOfCandy*0.01));
while (yourMoney >= priceOfCandy) {
candiesYouBuy++;
yourMoney -= priceOfCandy;
}
System.out.println("According to our calculation, you can buy " + candiesYouBuy + " candies and you have $" +
(yourMoney *0.01) + " left in your pocket.");
}
```

__Output:__

*You have $1.0 in your pocket.*

*Candies are priced at $0.1*

*According to our calculation, you can buy 10 candies and you have $0.0 left in your pocket.*

The processing time difference between BigDecimal and int types can be seen in the following program outputs by increasing your money up to $1.000.000 :

__Output(BigDecimal):__

*You have $1000000.0 in your pocket.*

*Candies are priced at $0.1.*

*According to our calculation, you can buy 10000000 candies and you have $0.0 left in your pocket.*

*processing time: 460 ms*

__Output(int):__

*You have $1000000.0 in your pocket.*

*Candies are priced at $0.1.*

*According to our calculation, you can buy 10000000 candies and you have $0.0 left in your pocket.*

processing time: 12 ms

Resources

Using double/long vs BigDecimal for monetary calculations

Efective Java 2nd Edition, J. Bloch

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}