# [SOLVED] rounding numbers in java



## raident30

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


----------



## Special2God

*Re: rounding numbers in java*

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.


----------



## raident30

*Re: rounding numbers in java*

how do i write codes here sir? i wanna show u where i want to put these codes...


----------



## Crockeo

*Re: rounding numbers in java*

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.


----------



## raident30

*Re: rounding numbers in java*



Special2God said:


> 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...


----------



## Special2God

*Re: rounding numbers in java*

Post code by using the "#" symbol in the reply editor, or type a pair of code tags like this


HTML:


[CODE]...[/CODE]

and put your code between the two tags at the point marked "..."



> 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?


----------



## raident30

*Re: rounding numbers in java*

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);

    }

}


----------



## Special2God

*Re: rounding numbers in java*

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?


----------



## raident30

*Re: rounding numbers in java*

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


----------



## Special2God

*Re: rounding numbers in java*

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


----------



## raident30

*Re: rounding numbers in java*

thanks a lot sir! it run smoothly! thanks thanks thanks thanks! many thanks!


----------



## Special2God

*Re: rounding numbers in java*

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


----------



## raident30

*Re: rounding numbers in java*

alrighty! thanks again!


----------

