Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I am attempting to round a number (float) to 2 decimal digits in java. How is this possible? Thanks, -- Adrien Martel cs2
Post Follow-up to this messageOne approach would be to multiply the number by 100, cast it to an int, then divide it by 100.0. This would truncate instead of round.
Post Follow-up to this message> I am attempting to round a number (float) to 2 decimal digits in java. How > is this possible? http://www.xdweb.net/~dibblego/java...nswers.html#q28 -- Tony Morris http://xdweb.net/~dibblego/
Post Follow-up to this message<klynn47@comcast.net> schreef in bericht news:1103571572.279608.114830@c13g2000cwb.googlegroups.com... > One approach would be to multiply the number by 100, cast it to an int, > then divide it by 100.0. This would truncate instead of round. Add 50 before multiplying and you will effectively round. Note that a float itself can never be rounded to exactly two decimal digits, unless the correctly rounded fraction is .0, .25, .50 or .75. This is because float an d double are numbers of base 2, not of base 10.
Post Follow-up to this messageAdrien Martel wrote: > Hi, > > I am attempting to round a number (float) to 2 decimal digits in java. How > is this possible? > > Thanks, > > -- > > Adrien Martel > cs2 > > Check out the NumberFormat class. java.text.NumberFormat in the API. If you have any trouble figuring out how to use it let us know. I'd be happy to give you an example. Paul
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.