wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Rounding a Double...to the nearest Integer, Tenths place, Hundredths place, etc.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rounding a Double...to the nearest Integer, Tenths place, Hundredths place, etc.." Watch "Rounding a Double...to the nearest Integer, Tenths place, Hundredths place, etc.." New topic
Author

Rounding a Double...to the nearest Integer, Tenths place, Hundredths place, etc..

Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Hello. I need some help! I am trying to have a program where the user can input a value, say 7, and have the code round the value to the nearest integer, nearest tenths place, nearest hundredths place and nearest thousandths place. Here is my code.
import java.awt.Graphics;
import javax.swing.*;

public class RoundingApplet extends JApplet

{
double number1;
float result1;
float result2;
float result3;
float result4;

public void init()
{
String firstNumber;
firstNumber = JOptionPane.showInputDialog("Enter a value to be rounded.");

number1 = Double.parseDouble(firstNumber);
roundtointeger();
roundtotenths();
roundtohundredths();
roundtothousandths();
}
public long roundtointeger()
{
result1 = Math.floor(number1+0.5);
return result1;
}
public long roundtotenths()
{
result2 = Math.floor(number1*10+0.5)/10;
return result2;
}
public long roundtohundredths()
{
result3 = Math.floor(number1*100+0.5)/100;
return result3;
}
public long roundtothousandths()
{
result4 = Math.floor(number1*1000+0.5)/1000;
return result4;
}
public void paint (Graphics g)
{
super.paint (g);
g.drawRect(15,10,400,105);
g.drawString("The original value is " + number1,25,25);
g.drawString("The value rounded to the nearest integer is " + result1,45,45);
g.drawString("The value rounded to the nearest tenth is " + result2,65,65);
g.drawString("The value rounded to the nearest hundredth is " + result3,85,85);
g.drawString("The value rounded to the nearest thousandth is " + result4,105,105);
}
}

Now, when I enter this code... everything runs fine, except that my value that I entered is not changed, it remains the (7.0) for example. I know that I have to cast a double to a float, like below....
float f;
double d=number1
f=(float)d;
but how do I do this so that when I do run the program it will output the correct rounding functions??
Please help ASAP!!
Thank you so much.
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
ACTUALLY....this is the code......corrected, please look at this one...and see what I should do!
import java.awt.Graphics;
import javax.swing.*;

public class RoundingApplet extends JApplet

{
double number1;
float result1;
float result2;
float result3;
float result4;
float result1;
double d=number1;
result1=(float)d;
float result2;
double d=number1;
result2=(float)d;
float result3;
double d=number1;
result3=(float)d;
float result4;
double d=number1;
result4=(float)d;
public void init()
{
String firstNumber;
firstNumber = JOptionPane.showInputDialog("Enter a value to be rounded.");

number1 = Double.parseDouble(firstNumber);
roundtointeger();
roundtotenths();
roundtohundredths();
roundtothousandths();
}
public float roundtointeger()
{
result1 = Math.floor(number1+0.5);
return result1;
}
public float roundtotenths()
{
result2 = Math.floor(number1*10+0.5)/10;
return result2;
}
public float roundtohundredths()
{
result3 = Math.floor(number1*100+0.5)/100;
return result3;
}
public float roundtothousandths()
{
result4 = Math.floor(number1*1000+0.5)/1000;
return result4;
}
public void paint (Graphics g)
{
super.paint (g);
g.drawRect(15,10,400,105);
g.drawString("The original value is " + number1,25,25);
g.drawString("The value rounded to the nearest integer is " + result1,45,45);
g.drawString("The value rounded to the nearest tenth is " + result2,65,65);
g.drawString("The value rounded to the nearest hundredth is " + result3,85,85);
g.drawString("The value rounded to the nearest thousandth is " + result4,105,105);
}
}
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Can you give a little more info? It sounds like you're saying that you've only tested this with the number 7.0. If so, you're program is running correctly. You should test this program by entering the number 5.1465. Then your output should be 5, 5.1, 5.15, 5.147, etc. I just did a program exactly like this and your code looks correct. What exactly is not working?
P.S. I'll test this code after I post this...wrong order, I know....but....


David Crossett
-nothing important to say, but learnin' plenty-
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
This code works fine if you make a couple of changes. Change your variables to doubles, and change the return types of your methods to doubles ( any reason you chose long? ). Here is the working code:

Hope this helps!
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
I think I got it, yeah me being stupid.....lol, sorry about that, I guess i paniced, I had to change it back to doubles for it to work right, but I did get the correct output when I entered like 4.6576 instead of just an integer. My god, I must be tired!! *blushing* forgive me........hahahhaha
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Sorry about the multiple posts...I figured out what you're talking about. HERE is the correct code to make the integer show without decimal points AND I correct some of your naming conventions ( notice the methods being capitalized, etc. ) AND I called the methods in the print statement to make the code a little sleeker.
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Sorry...there is a mistake in that last code...in the paint() method, I accidentally called roundToInteger() to display the original number...just remove that to leave...


..and it will all work. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Rounding a Double...to the nearest Integer, Tenths place, Hundredths place, etc..
 
Similar Threads
STRUTS 2 ERROR
casting from a JTextField to a float?
rounding a double
BinarySearch() Question
Math.round(20.5) & Math.round(-20.5)