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
Marshal
Joined: Mar 22, 2005
Posts: 35247
7
posted
0
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.
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.
Never ascribe to malice that which can be adequately explained by stupidity.
Al Hollis
Ranch Hand
Joined: Dec 12, 2005
Posts: 60
posted
0
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
Joined: Feb 03, 2004
Posts: 456
posted
0
... anyhow, use a Price / Currency class. takes 5 minutes to create, and potentially saves you a lot of hassle.
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
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
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
Joined: Apr 27, 2006
Posts: 62
posted
0
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
Could try harder
Al Hollis
Ranch Hand
Joined: Dec 12, 2005
Posts: 60
posted
0
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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Vlado Zajac
Ranch Hand
Joined: Aug 03, 2004
Posts: 244
posted
0
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.