Go Back   Tech Support Forum > The IT Pro > Programming

[SOLVED] rounding numbers in java

This is a discussion on [SOLVED] rounding numbers in java within the Programming forums, part of the Tech Support Forum category. How do i write a program that prompts the user to input a decimal number and outputs the number through


Reply
 
Thread Tools Search this Thread
Old 07-18-2011, 06:44 PM   #1
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


How do i write a program that prompts the user to input a decimal number and outputs the number through the nearest integer. thanks

__________________
raident30 is offline   Reply With Quote
Old 07-19-2011, 08:49 AM   #2
Games Team | Tech
 
Special2God's Avatar
 
Join Date: Oct 2009
Location: Earth
Posts: 4,414
OS: Win 7 (x64)

My System


According to the title of your thread you want to round in java.
You should have an int value for the rounded value
and a double value for the user's input, then do something like this:
Code:
int roundedValue;
double input;
//... get user input
roundedValue = (int) input;
Notice that this rounds 4.3, 4.5, and 4.9 all to 4.
If you want to round a value like 4.5 to 5 you could do something like:
Code:
	roundedValue = (int) Math.round(input);
Or create your own little conversion possibly like this:

Code:
if((input - roundedValue) >= 0.5 ) {
	roundedValue += 1;
}
You can do this however you want, but basically you can let Java do it for you with Math functions or you can test your value for a decimal larger than .5 and convert it up to the next value and let the (int) conversion convert all values less than .5 down to the previous value.
Hope this helps, please post back if you have questions or concerns.

__________________
Special2God is offline   Reply With Quote
Old 07-19-2011, 06:19 PM   #3
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


how do i write codes here sir? i wanna show u where i want to put these codes...
__________________
raident30 is offline   Reply With Quote
Old 07-19-2011, 08:14 PM   #4
Registered Member
 
Join Date: Apr 2010
Posts: 236
OS: Windows 7

My System


This is how I would do something like what the previous person said:

Method 1:
Code:
//Something like:

Scanner in = new Scanner(System.in);
String in = in.nextLine();
try
{
int inInt = Integer.parseInt(in);
}
catch (Exception e)
{
System.out.println("Input is not a number!");
}
I didn't put any indentations because I was working with the text editor on this forum.
__________________
Crockeo is offline   Reply With Quote
Old 07-20-2011, 04:07 AM   #5
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


Quote:
Originally Posted by Special2God View Post
According to the title of your thread you want to round in java.
You should have an int value for the rounded value
and a double value for the user's input, then do something like this:
Code:
int roundedValue;
double input;
//... get user input
roundedValue = (int) input;
Notice that this rounds 4.3, 4.5, and 4.9 all to 4.
If you want to round a value like 4.5 to 5 you could do something like:
Code:
	roundedValue = (int) Math.round(input);
Or create your own little conversion possibly like this:

Code:
if((input - roundedValue) >= 0.5 ) {
	roundedValue += 1;
}
You can do this however you want, but basically you can let Java do it for you with Math functions or you can test your value for a decimal larger than .5 and convert it up to the next value and let the (int) conversion convert all values less than .5 down to the previous value.
Hope this helps, please post back if you have questions or concerns.
sir how do i declare the int here? so that it'll just prompt me to type a number with decimal...
__________________
raident30 is offline   Reply With Quote
Old 07-20-2011, 05:52 PM   #6
Games Team | Tech
 
Special2God's Avatar
 
Join Date: Oct 2009
Location: Earth
Posts: 4,414
OS: Win 7 (x64)

My System


Post code by using the "#" symbol in the reply editor, or type a pair of code tags like this
HTML Code:
[CODE]...[/CODE]
and put your code between the two tags at the point marked "..."

Quote:
sir how do i declare the int here? so that it'll just prompt me to type a number with decimal...
I am not sure that I understand your question.
Are you asking how to retrieve a value the user entered on the keyboard or are you ask how to tell the user you want a decimal?
__________________
Special2God is offline   Reply With Quote
Old 07-22-2011, 04:05 AM   #7
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


sorry about that.. ill rephrase it... something like this...

Code:
int = num1
int = num2

num1 = 10
num2 = 20
and how do i put those codes here?

Code:
import java.util.*;
public class Mystery {

	static scanner console = new Scanner(System.in);
	
	static final int NUM = 10;
	static final double X = 20.5;
     
      public static void main(String[] args)

      {

	int a, b;
	double z;
	char grade;

	a = 25;

	System.out.println("a = " + a);

	System.out.println("Enter the first integers: ");
	a = console.nextInt();
	System.out.println();
	
	System.out.println ("Enter second integers: ");
	b = console.nextInt();
	System.out.println();
	
	System.out.println("The numbers you entered are " + a + " and " + b);
	
	z = X + 2 * a - b;
	
	System.out.println("z = " + z);

	grade = 'A';
	System.out.println("Your grade is " + grade);

	a = 2 * NUM + (int) z;
	System.out.println("The value of a = " + a);

    }

}
__________________
raident30 is offline   Reply With Quote
Old 07-22-2011, 06:40 AM   #8
Games Team | Tech
 
Special2God's Avatar
 
Join Date: Oct 2009
Location: Earth
Posts: 4,414
OS: Win 7 (x64)

My System


Your Code appears to be correct, what is the problem?
(Note: I capitalized Scanner since java requires object types to be capitalized the same way you capitalize String)
Code:
import java.util.*;
public class Mystery {

	// REMEMBER TO CAPITALIZE OBJECT TYPES LIKE Scanner
	static Scanner console = new Scanner(System.in);
	
	static final int NUM = 10;
	static final double X = 20.5;
     
      public static void main(String[] args)

      {

	int a, b;
	double z;
	char grade;

	a = 25;

	System.out.println("a = " + a);

	System.out.println("Enter the first integers: ");
	a = console.nextInt();
	System.out.println();
	
	System.out.println ("Enter second integers: ");
	b = console.nextInt();
	System.out.println();
	
	System.out.println("The numbers you entered are " + a + " and " + b);
	
	z = X + 2 * a - b;
	
	System.out.println("z = " + z);

	grade = 'A';
	System.out.println("Your grade is " + grade);

	a = 2 * NUM + (int) z;
	System.out.println("The value of a = " + a);

    }

}
I am sorry, but I still do not understand your questions,
your code works perfectly, what were you trying to add or change?
__________________
Special2God is offline   Reply With Quote
Old 07-22-2011, 07:18 AM   #9
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


i want to add the codes you gave me that if the user put a decimal numbers on the grades, it'll just round it to the nearest integer.... sorry im still a beginner in programming
__________________
raident30 is offline   Reply With Quote
Old 07-22-2011, 10:16 AM   #10
Games Team | Tech
 
Special2God's Avatar
 
Join Date: Oct 2009
Location: Earth
Posts: 4,414
OS: Win 7 (x64)

My System


This should be what you want.
It excepts an int or a decimal number and rounds all values.
Code:
import java.util.*;
public class Mystery {

	static Scanner console = new Scanner(System.in);

	static final int NUM = 10;
	static final double X = 20.5;

	public static void main(String[] args)
	{

		int a, b;
		double z;
		char grade;
		double input; // New temporary variable that holds user's input before we round

		a = 25;

		System.out.println("a = " + a);

		System.out.println("\nEnter the first integers: ");

		// Here we get a double from the keyboard and store it in our temporary input variable,
		// then we round it to an integer value, 
		// you probably noticed previously that if you tried to enter an integer the program would crash
		input = console.nextDouble();
		a = (int) Math.round(input);


		System.out.println ("\nEnter second integers: ");

		// Here we get a double from the keyboard and store it in our temporary input variable,
		// then we round it to an integer value, 
		// you probably noticed previously that if you tried to enter an integer the program would crash
		input = console.nextDouble();
		b = (int) Math.round(input);


		System.out.println("\nThe numbers you entered are " + a + " and " + b);

		z = X + 2 * a - b;
		System.out.println("\nz = " + z);

		grade = 'A';
		System.out.println("Your grade is " + grade);

		a = 2 * NUM + (int) z;
		System.out.println("The value of a = " + a);

	}

}
How this is what you needed :)
__________________
Special2God is offline   Reply With Quote
Old 07-23-2011, 03:30 AM   #11
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


thanks a lot sir! it run smoothly! thanks thanks thanks thanks! many thanks!
__________________
raident30 is offline   Reply With Quote
Old 07-23-2011, 03:32 PM   #12
Games Team | Tech
 
Special2God's Avatar
 
Join Date: Oct 2009
Location: Earth
Posts: 4,414
OS: Win 7 (x64)

My System


Glad to be of service, if your original question is solved,
please go to the top of this thread and click on "thread tools" > "mark thread as solved"
to mark this thread as solved so that people know your question was answered.
TY and happy programming :)
__________________
Special2God is offline   Reply With Quote
Old 07-23-2011, 10:34 PM   #13
Registered Member
 
raident30's Avatar
 
Join Date: Jun 2009
Location: philippines
Posts: 404
OS: Windom 7 Ultimate 64bit

My System


alrighty! thanks again!

__________________
raident30 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
dwm.exe / csrss.exe / conhost.exe?
I am trying to clean up this computer for a friend - unfortunately someone else has already been messing around with it and trying to sort it out (I found various cleanup programs on the desktop) but to no avail. As far as I can see/have been told the symptoms have been - Hiding all documents...
lm03929z Resolved HJT Threads 20 05-09-2011 03:42 PM
.dll files missing, browser opens new tabs, google search redirects.
Hello, I'm a complete computer novice, but I know things are not right. At startup I get two pop-ups stating some .dll files are missing. I've googled these files and only got a couple of hits, it seems they're some kind of virus. My browser also opens up new tabs on it's own, and google search...
jtatauburn Resolved HJT Threads 24 04-02-2011 09:38 PM
Browser Redirect Issue
I have been having an issue with both IE and Firefox redirecting Google search results a majority of the time. I had done a scan with Spybot Search & Destroy prior to posting here and "Fraud.WindowsProtectionSuite" (15 entries) and "Microsoft.Windows.RedirectedHosts" (3 entries) were the only...
bob2881 Resolved HJT Threads 21 02-21-2011 06:48 PM

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2


All times are GMT -7. The time now is 02:06 AM.


Copyright 2001 - 2014, Tech Support Forum

Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts