Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

rounding
Hi,

I am attempting to round a number (float) to 2 decimal digits in java. How
is this possible?

Thanks,

--

Adrien Martel
cs2



Report this thread to moderator Post Follow-up to this message
Old Post
Adrien Martel
12-20-04 09:00 PM


Re: rounding
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.


Report this thread to moderator Post Follow-up to this message
Old Post
klynn47@comcast.net
12-21-04 01:58 AM


Re: rounding
> 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/



Report this thread to moderator Post Follow-up to this message
Old Post
Tony Morris
12-21-04 01:58 AM


Re: rounding
<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.



Report this thread to moderator Post Follow-up to this message
Old Post
Boudewijn Dijkstra
12-21-04 01:57 PM


Re: rounding
Adrien 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


Report this thread to moderator Post Follow-up to this message
Old Post
Paul O'Donnell
12-22-04 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:54 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.