Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Rounding up an integar

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 08-24-2012, 05:17 AM PM User | #1 Atlan New Coder   Join Date: Jul 2012 Posts: 15 Thanks: 2 Thanked 0 Times in 0 Posts Rounding up an integar Hi all I'm trying to write a small function which has to, at one point, round a number UP to the nearest multiple of 5. In theory, the results should be: 4 rounds up to 5 5 rounds up to 5 6 rounds up to 10 7 rounds up to 10 8 rounds up to 10 9 rounds up to 10 10 rounds up to 10 After a fair bit of experimentation, I ended up with the following, but it doesn't work properly. Code: for(int i = 0; i < 40; i++) { int temp = (int) (Math.ceil(i/5)*5); System.out.println("I = " + i + ", rounded = " + temp); } This code rounds DOWN, so 9 rounds down to 5. Anyone know how to make it round UP?
 08-24-2012, 06:18 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,598 Times in 2,567 Posts You're ceil call is going against an integer value. 6 / 5 is 1, multiplied by 5 is 5. You need to convert that to a double, so 6 / 5.0 is 1.2 rounded up is 2 * 5. So simply divide by 5.0 to force an implicit cast to a double.
 Users who have thanked Fou-Lu for this post: Atlan (08-24-2012)
 08-24-2012, 06:55 AM PM User | #3 Atlan New Coder   Join Date: Jul 2012 Posts: 15 Thanks: 2 Thanked 0 Times in 0 Posts Complete success! Thanks a bunch.

 Bookmarks

 Tags java, math, round, round up, rounding

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:11 PM.