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
Solved

How do I round a number (Simple Java Calculator)

Posted on 2004-05-03
7
1,784 Views
Last Modified: 2008-02-01
Hello

I have built a calculator that is pretty much complete but for the inability to round numbers

what I want to do is say input a number x (double) press the "round" key, enter another number(y double and converted to int)  and that number will be rounded to that number (y).

Example

 9.4524 is entered (x)
 Press Round Button
 2 is entered (y)
 
 result = 9.45 (9.4524 to y decimal places)

I have been trying to implement this but with out success

Here is some "example Code"

public double Rounded(Double d, int i){
    BigDecimal rnd = new BigDecimal(d); /* the constructor is the only way I could find to convert */
    rnd.setScale(i);                                /* a double to BigDecimal format */
    return rnd.doubleValue();
}

Cheers
Murray S

PS I have little in the way of knowlege re: BigDecimal so quick tutorial would be good
0
Comment
Question by:Murray_S
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 10975391
There might be a better way, but this works :)

double round(double d, int i) {
   return ((int)((d + 5*Math.pow(10,-(i+1))) * Math.pow(10,i))/Math.pow(10,i));
}
0
 

Author Comment

by:Murray_S
ID: 10975452
Yep this works

But could you explain it to me please, how did you come up with this algorithm &c.

Cheers
Murray
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 10975649
basically, it does this:

take the following number:
1.45643

If you want 2 decimal places, you can multiply the number by 100 to get:
145.643

now if you cast that to an integer, you will get
145

Last of all, wel divide by 100 to get
1.45

That gives us the original number to two decimal places.  You also notice that first we add .005 to the number we are going to round, that way it rounds to the nearest integer when you cast it.  So if the original number is 1.45643, and you are rounding to two decimal places, you get 1.46 instead of 1.45.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 5

Accepted Solution

by:
lwinkenb earned 200 total points
ID: 10975694
I'll write out the algorithm so it's easier to see

double d = 1.234567;
int i = 4;
d = d + 5*10^(-i - 1);  // that would give us d = d + .00005, or d = 1.234617;
d = d * 10^i; // that gives us d = d * 10000, or d = 12346.17;
d = (int)d;  // d = 12346
d = d / 10^i; // d = d / 10000, or d = 1.2346;  That is the answer we want!
0
 

Expert Comment

by:rubentrancoso
ID: 10983350
just a question,
isn´t it to steps and intensive processing to get the result?
I want to use it to round the mouse move and place a component on the proper grid position. It wont slow down the operation?
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 10983461
If all you're doing is rounding mouse movement so that you can "snap" on to a grid, then why do you need be able to round to a variable number of decimal places?

If you knew the number of decimal places to round to before-hand, then you could make the algorithm much simpler by just doing:

d = Math.round(d * 100.0) / 100.0;  // This does two decimal places
0
 

Expert Comment

by:rubentrancoso
ID: 10983943
As I can figure out it´s what I´m trying...

holdX and holdY are offsets from component origin to mouse positon.

    public void setSnapLocation( int x, int y, int gridX, int gridY ) {
        double ox, oy;
        double gx, gy;
        gx = gridX * 1.0;
        gy = gridY * 1.0;
        ox = Math.round( ( ( x - holdX ) * 1.0 + gx / 2  ) / gx ) * gx;
        oy = Math.round( ( ( y - holdY ) * 1.0 + gy / 2  ) / gy ) * gy;
        super.setLocation( (int)ox-GRABBERMARGIN, (int)oy-GRABBERMARGIN );
    }

it´s a rubber move from mouse catch to every sides.
maybe I can give you some points to :)
thanks
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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 61
table example 4 32
MySqlDump not dumping triggers 1 43
Java Restore security prompts not working 10 53
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

809 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