How would you round a double value to certain decimal Precision and Scale ?

By | November 26, 2019

In this post we will discuss about one of the basic  java interview question and question is that How would you round a double value to certain decimal Precision and Scale ?

First of all let us understand what’s the difference between Precision and Scale.

Precision represent total number of digits (or significant digits) of a real number and Scale specifies number of digits after decimal place
For example, 91.763 has precision of 5 and scale of 3

No body wants to loose the precision value of the number as it will change the value by large amount. If you still  willing  to loose the precision simply you can divide the number by 10 to the power precision.
There are several ways in Java to round the double value to certain scale, as  shown in the below example

Client program: 

Output:

8754.33
8754.33
8754.33

You may also like:

Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?
Can we call static method with null object?
Can we override static method in Java?
What will be the output of following java program?
What is the difference Between java.util.Date and java.sql.Date in Java ?
What is difference between using Serializable & Externalizable Interfaces in Java?

That’s all about How would you round a double value to certain decimal Precision and Scale ?
If you have any feedback or suggestion please feel free to drop in blow comment box. 

Leave a Reply

Your email address will not be published.