File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Float decimal points.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Float decimal points.." Watch "Float decimal points.." New topic
Author

Float decimal points..

Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
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
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.


Android appsImageJ pluginsJava web charts
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
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

Joined: Oct 17, 2005
Posts: 143
As mentioned above you can use the DecimalFormat class:



Stephen Foy - Microsoft Application Development Consultant
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
thanks very much. Spot on!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

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
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
... 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

Joined: Oct 02, 2003
Posts: 9948
    
    6

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
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
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
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
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
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Float decimal points..
 
Similar Threads
output
Float
a program to check input is integer, float, or NAN
Float
assignment 2.2