• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic