?
Solved

Rounding integer value

Posted on 1998-11-04
7
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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