• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Money value object

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not allow negative values, but never never use floating-point arithmetic for money. Use integer arithmetic or java.math.BigDecimal.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also follow Campbell's advice to throwing an exception:
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome . . . and thanks Rob and Jesper too.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I must! Thanks also to Jesper & Rob for advices.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic