-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Financial operation
PostPosted: Wed Nov 21, 2007 5:08 pm 
Newbie

Joined: Tue May 22, 2007 3:39 am
Posts: 15
Financial operation.

I'm wondering about mapping type for ammount money in database defined as
numeric(10,2).
I have two option:

1. Mapping to BigDecimal

2. Maping to Double
In this case I'd like move access type to field and give
Long getter and setter in which is doing appropriate conversion from/to Double (with multiply/ divide 100):

//return salary in cent
public Long getSalary() {
return Util.getAsLong(amount);
}

public void setSalary(Long ammount) {
this.amount = Util.getAsDouble(amount);
}
In this case all financial operation are doing on Long (cents).


I know that using BigDecimal is comfortable solution buy not most efficient and fast.
What do you think about solution witch Double ?
What are you using for financial operations ?

Thanks in advance
gienas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 5:34 am 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
Use BigDecimal or use Long for cents. I think using Double for financial data is a very, very bad idea. Efficiency is far less important than correctness in finance, no?


Top
 Profile  
 
 Post subject: Re: Financial operation
PostPosted: Fri May 18, 2012 4:01 am 
Newbie

Joined: Wed Feb 29, 2012 4:21 am
Posts: 2
In fact, using wrong constructor of BigDecmial is a typical mistake that Java Programmers make. BigDecimal has overloaded constructor and if you use the one which accept double as argument you will get same result as you do while operating with double. So always use BigDecimal with String constructor. Here is an example of using BigDecmial constructed with double values:

//Creating BigDecimal from double values
BigDecimal amount3 = new BigDecimal(2.15);
BigDecimal amount4 = new BigDecimal(1.10) ;
System.out.println("difference between 2.15 and 1.0 using BigDecmial is: " + (amount3.subtract(amount4)));

Output:
difference between 2.15 and 1.0 using double is: 1.0499999999999998
difference between 2.15 and 1.0 using BigDecmial is: 1.049999999999999822364316059974953532218933105468750

_________________
It is very important to stay afloat in our economically hard time. That is why getting short term cash advance with bad credit may help you fight money difficulties.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.