Data types: currency vs float

8
We got a guestion from a customer for whom we're building an application. They looked at the database generated by Mendix and saw Mendix is using the same SQL data types for storing float and currency. --> Float. They mentioned that float values may never be used for currency values due to the following explained here: http://msdn.microsoft.com/en-us/library/ms173773.aspx. A float data type in the database is actually an approximate value. Now our question is: How does Mendix handle these two data types? In the database and in the forms? What are the differences? And, what can we reply to the customer for this issue? The example used by the customer is shown below. Here is a simple test written in Java (Mendix runs on Java) : public class TestFloat { public static void main(String[] args) { for (float value = 0.0f; value < 1.0f; value += 0.1f) { System.out.println(value); } } } The output : 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.70000005 0.8000001 0.9000001
asked
1 answers
4

Currently the Currency and Float attribute types only differ in the representation in the web client, a Currency is automatically rounded to 2 digits. This means Currency is also represented with a Float / Double in Java and the database.

If you do a lot of calculations Floats will become inaccurate, therefore it is adviced to use Long/Integer or, in your Java code BigDecimal. We will investigate if we can by default use BigDecimal for Currency attribute types. To track progress on this subject please file a ticket.

Edit: see also comment of Arjen "With regard to what the web client shows for float values: before sending float values to the client the runtime rounds the values using BigDecimal.round with a precision of 10. So if you need more precision than this follow Johan's advice."

answered