Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Rounding Decimal Numbers?
Author Message
Delta

Posted: Thu Jul 01, 2004 1:43 pm   Post subject: Rounding Decimal Numbers?

How do you round to decimal places?
for example how do I round

8.49999999
to
8.5

Please explain. Thank you.

Paul

Posted: Thu Jul 01, 2004 2:16 pm   Post subject: (No subject)

isn't it Math.round?
like
 code: System.out.println(x + " is approximately " + Math.round(x));
Tony

Posted: Thu Jul 01, 2004 2:53 pm   Post subject: (No subject)

no, Math.round would round to an integer. What you do is you multiply it by 10^number of decimal places, round, and then divide back.

such as
8.499 *10^1
84.99 ~ round
85 /10^1
8.5
Tony's programming blog. DWITE - a programming contest.
Delta

Posted: Thu Jul 01, 2004 3:28 pm   Post subject: (No subject)

That's the thing tony... I asked how to round...
Tony

Posted: Thu Jul 01, 2004 3:30 pm   Post subject: (No subject)

i donno... you can typecast it to an (int)
Tony's programming blog. DWITE - a programming contest.
wtd

Posted: Thu Jul 01, 2004 4:13 pm   Post subject: (No subject)

Do you just need this for formatting text?
zylum

Posted: Fri Jul 02, 2004 1:10 pm   Post subject: (No subject)

tony wrote:
no, Math.round would round to an integer. What you do is you multiply it by 10^number of decimal places, round, and then divide back.

such as
8.499 *10^1
84.99 ~ round
85 /10^1
8.5

i think tony's way is the only way to do it unless there is some weird function i havent heard of...

 code: double num = 8.4999; num *= 10; num = Math.round(num); num /= 10; // or just num = Math.round(num * 10) / 10; System.out.println(num); // gets you 8.5
Dan

Posted: Fri Jul 02, 2004 2:12 pm   Post subject: (No subject)

Thats how i do it also. Alougth u whould think there whould be a method for it some where in one of thous java APIs.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!

LiquidDragon

Posted: Fri Jul 09, 2004 7:26 pm   Post subject: (No subject)

I always thought there was a round method. But i may be wrong
zylum

Posted: Fri Jul 09, 2004 11:42 pm   Post subject: (No subject)

there is a round method but it rounds to the nearest one not the nearest tenth.
Delta

Posted: Sat Jul 24, 2004 8:13 am   Post subject: (No subject)

Ok well I found some nice lil rounding methods... (I believe this was just for rounding so I could display the text)

 code: import java.text.*; DecimalFormat decF = new DecimalFormat ("###.##"); System.out.println (decF (99999.999999));

also

 code: decF.setMinimumFractionDigits(2);

would set the format to a minimum of two decimal places

 code: decF.setMaximumFractionDigits(2);

would set the format to a maximum of two decimal places

The decimal format thing is only for text as far as I know (for outting it)...

but when it came to storing the value I usually ended up using 'long' instead of 'int' or 'double'... because long would actually round how I wanted it to... never the less... thanks for the help.... Have a nice day.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 11 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: