Math.round method rounds the decimal to an integer by stripping off the decimal places. How can we use the same method to keep only 2 decimal places<br /> <br /> For example -<br /> <br /> Math.round(12.3456) will return 12. What should we do to get 12.34 without using any other class or method.

# Search Interview Questions

More than 3000 questions in repository.
There are more than 900 unanswered questions.
Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

Java - Interview Questions and Answers

Q1. Math.round method rounds the decimal to an integer by stripping off the decimal places. How can we use the same method to keep only 2 decimal places

For example -

Math.round(12.3456) will return 12. What should we do to get 12.34 without using any other class or method.
Core Java2018-05-19 07:48:35
Ans. We can multiply the value by 100 and then use Math.round on that and then divide the result by 100

For example -

(Math.round(12.3456 * 100)) / 100

Help us improve. Please let us know the company, where you were asked this question :

Math.round  double

Related Questions

What is the difference between float and double?
What is the difference between these two approaches of creating singleton Class ?

//Double Checked Locking Code
public static Singleton createInstance() {
if(singleton == null){
synchronized(Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}

//Single checked locking code
public static Singleton createInstance() {
synchronized(Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
return singleton;
}
Have you ever faced problems with rounding decimal places and what were the reasons ?
Difference between DoubleSummaryStatistics , IntSummaryStatistics and LongSummaryStatistics ?
Name few Double data types related classes and interfaces ?
Which of the following data type you will prefer to store price - BigDecimal or Double ?
What is the size of double type ?
Why calculations in float or Double sometimes gives unpredictable results ?
What is the problem with this code

double x=10.04;
int a = x;
What is a double ended queue?

## Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.