It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Money value object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Money value object" Watch "Money value object" New topic
Author

Money value object

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Currently my Money class has a constructor of the form:

I am currently disallowing negative amounts. Basically if you pass in a negative amount, I will take the absolute value and represent it as a positive value instead.

Do you think I should allow negative Money values ?

regards,

Pho
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Why not allow negative values, but never never use floating-point arithmetic for money. Use integer arithmetic or java.math.BigDecimal.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And if you regard a value as invalid, don't alter it or take its absolute value.

Refuse it and throw an Exception instead.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

Whether you should allow negative values depends on the application that you intend to use it for.

But a more important point is what Campbell already mentioned: Do not use floating-point types (float and double) for dealing with money. Floating-point types have a limited precision, and if you do calculations with them you can quickly get rounding errors, which is a problem when dealing with money.

Instead of using float or double, use int or long and store the amount in cents instead of dollars (or whatever currency you're using), or use BigDecimal (which has arbitrary precision).
[ November 28, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

And also follow Campbell's advice to throwing an exception:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Thanks for the messages.

@Jesper,

Whether you should allow negative values depends on the application that you intend to use it for.

I'm working on an application that allows people to pay (via paypal) and exchange that amount for credits to make voice over IP calls. I guess my question is what applications would require negative Money amounts ?

Regards,

Pho
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
That suggests your application forbids non-positive values and you should throw exceptions for negative values, and maybe also for zero values.
Remember there is a static constant BigDecimal object called ZERO.
Remember also that the equals method in BigDecimal takes precision into account, so new BigDecimal("0.00").equals(new BigDecimal("0.0")) returns false. You need the compareTo which Rob Prime showed earlier.
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Thanks Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You're welcome . . . and thanks Rob and Jesper too.
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Yes I must! Thanks also to Jesper & Rob for advices.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Money value object
 
Similar Threads
Value objects and encapsulation
Implementation of a "Money" value object
[Struts] How do you handle non-text data entry in your Struts forms?
Handling currency throughout an application
JSF Currency Conversion