Solved

Rounding integer value

Posted on 1998-11-04
7
245 Views
Last Modified: 2010-08-05
How can can I round integer values f.ex.
768 to 800 and 732 to 700 or 56 to 60 ...
0
Comment
Question by:tittapo
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:omok
ID: 1226917
Try this method:
private int round( double num )
{
      if ( num >= 10 ) {
            return round( (double)num / 10 ) * 10;
      }
      return (int)Math.round(num);
}
Hope this helps.
0
 

Author Comment

by:tittapo
ID: 1226918
hey omok.
your code gave me number 689 when I called it with 689 not 700.

private int round( double num )
   {
   if ( num >= 10 ) {
   return round( (double)num / 10 ) * 10;
   }
   return (int)Math.round(num);
}
0
 
LVL 8

Accepted Solution

by:
diakov earned 20 total points
ID: 1226919
public static int round (int i)
{
  int mod = i % 100;
  return (mod >= 50)?(i + (100 - mod)):( i - mod);
}

This will accomplish your quest.

Cheers,
  Nik
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:tittapo
ID: 1226920
Comment.
Thank' s for answers.
Actually that code was usuful for my purpose.

if ((maxnumber>=100) && (maxnumber<1000))
          {
            maxnumber = (int)(maxnumber/100)*100;
          }
if ((maxnumber>=10) && (maxnumber<100))
          {
            maxnumber = (int)(maxnumber/10)  * 10;
          }

0
 
LVL 1

Expert Comment

by:omok
ID: 1226921
tittapo,
Are you sure my method doesn't work?
I have written a test program and tested it before I posted it here???
I am using JDK1.1.7 on NT4
0
 
LVL 1

Expert Comment

by:omok
ID: 1226922
tittapo,
I don't think your code will work as (maxnumber/10) will always round down because you are doing Integer division.
Try 49, it will give you 40 instead of 50.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1226923
public static int round (int i)
{
  if (i >= 100)
  {
    int mod = i % 100;
    return (mod >= 50)?(i + (100 - mod)):( i - mod);
  }
  else
  {
    int mod = i % 10;
    return (mod >= 5)?(i + (10 - mod)):( i - mod);
  }
}


2-nd attempt.

Cheers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 65
What is JNDI datasource in spring 1 38
wordlen challenge 3 49
eclipse java  build path 6 25
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now