• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float decimal points..  RSS feed

 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a quick way of making a float always have two decimal points. Even if its double 0. Im trying to represent currency and just cant get it working properley.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what "two decimal points" means (maybe two fractional digits?), but check out the java.text.DecimalFormat class. It lets you format decimal numbers with as many leading and trailing digits as you like.
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh sorry badly worded lol. I got some horrible flu thing so ill blame that.

What i mean is one decimal point. Two decimal places e.g 1.00 2.34 4.52 so it looks like currency
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned above you can use the DecimalFormat class:

 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much. Spot on!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a word of caution... it's generally rather risky to represent currency with a float. you generally want to store it as an int, and (for US), have it hold pennies.

for display purposes, you can then convert it.

i can't tell from you're post if you're doing this or not.

and for a small (possibly homework assignment), it probably doesn't matter.
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its for a fruit machine im creating as part of my final year project Basically im just using it just to add the winnings / earning of the machine to the balance which is also a float.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... anyhow, use a Price / Currency class. takes 5 minutes to create, and potentially saves you a lot of hassle.

that's my experience,

Jan
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as long as you are aware of potential problems, you'll probably be fine. but remember that floats are not actual representations of the values you think they are. when you think you have 1.01, you really have something like
1.010000000000007. if you add enough together, bad things can happen.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to print with two decimal places, try something likeIt only works in J5.0, however.

And I agree with the people who imply you shouldn't use a float for money in the first place.

for integer arithmetic tryJust make sure your total amount can never exceed approx $20000000

CR

Ouch. I have just noticed my mistake.
For "System.printf" read "System.out.printf."
CR
[ May 25, 2006: Message edited by: Campbell Ritchie ]
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I would use the BigDecimal class for currency, it takes care of all the floating point worries under the hood, and it allows you to perform mathematical functions.

Ramen
 
Al Hollis
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your suggestions As the machine isnt actually using real money im just going to keep it as a float for now and use one of the simpler suggestions from above I had noticed that about the float was quite interesting but for the purposes of the machine its just a representation of a number

Plus gives me something to comment on in the report don't it
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Time and Money code library is a nifty example of the Domain Driven Design or Domain Specific Language style. See how Gregor Hohpe describes his experience with the package as an introduction. It really points out how much Calendar bites.

Note well the caveat - the package is very cool, but hasn't been proven in production.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point numbers and not good for currency. Fixed point representation is much better. I would use int or long and store the amount in cents.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!