Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data type to use for manipulating currency

Tags:

java

currency

I am trying to decide which data type shall i use for a financial application.

I have read that Double or BigDecimal should be used. And i am confused between them. Any help in this regard will be highly appreciated

like image 579
Jabir Avatar asked Apr 07 '13 16:04

Jabir


People also ask

What data type should be used for currency?

The type-declaration character for Currency is the at (@) sign. The Currency data type is useful for calculations involving money and for fixed-point calculations in which accuracy is particularly important.

How do you add currency to a data type?

Select the cells and then select Insert > Table. Although creating a table isn't required, it'll make inserting data from the data type much easier later. With the cells still selected, go to the Data tab and select the Currencies data type.

What is the datatype for currency in Java?

1 Answer. Java has Currency class that represents the ISO 4217 currency codes. BigDecimal is the best type for representing currency decimal values.


1 Answers

You almost certainly don't want to use floating-point types (double, float, Double, Float) to handle monetary amounts, especially if you will be performing computations on them. The main reason for this is that there are many simple-looking numbers that cannot be represented exactly as a double et al. One such number is 0.1.

BigDecimal is therefore a much better choice for this use case.

like image 107
NPE Avatar answered Sep 20 '22 22:09

NPE