how to round off digit to 7 decimal place in java [duplicate] - java

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I am making a java program in which i have to round off the double digit to seven place but I don't know how to do it.
like 6.6666667e-10 to 0.0000001

The reason it return 0 is because the number example is too small. it can't be rounded to 7 digits after decimal point because the first nonzero digit in it's full decimal representation is after the 7th digit.
However, for slightly bigger numbers, this code should do the trick:
double a = 6.66666667E-10;
DecimalFormat df = new DecimalFormat("#.#######");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println(df.format(a));

Related

BigDecimal Round to 2 decimal places and then to one significant figure [duplicate]

This question already has answers here:
How to display a number with always 2 decimal points using BigDecimal?
(7 answers)
Closed 5 years ago.
How do i round a BigDecimal to 2 decimal places and then to one significant figure?
Thanks
You can do something like this using a class in android DecimalFormat.
private String formatMagnitude(double magnitude)
{
DecimalFormat magnitudeFormat = new DecimalFormat("0.00");
return magnitudeFormat.format(magnitude);
}

Java format GUI output [duplicate]

This question already has answers here:
How to format a number to Fixed length, space padded, thousand separator, 2 decimals in Java
(3 answers)
Closed 6 years ago.
If I want to format output in a GUI (specifically having only 2 decimal places for a number), would I use the DecimalFormat class along with the printf() method, or is there another way of doing this?
Basically you will want to round the number leaving 2 decimal places, or, in other words, round only the digits after the second digit after a decimal point, so:
number = Math.round(number * 100)/100;
Depending on your exact needs use Math.floor() or Math.ceil()

Expected Double value to display two zeros after precision value [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have a scenario where we need to display two zeros i.e 00 after the precision for double value. But unfortunately Java is not returning the second zero.
I have tried the below way but it is returning only one zero after decimal.
double inputvalue = 12345.00;
double d = ((BigDecimal.valueOf(inputvalue)).setScale(2,RoundingMode.UP)).doubleValue();
Expected output: 12345.00 but it is returning 1234.0.
Have you considered using a string to display the value. e.g.
String.format( "%.2f", d)

Round double value to 2 decimal digits [duplicate]

This question already has answers here:
Format double value in scientific notation
(4 answers)
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have been working with big numbers (with more that 20 digits) and for that reason I use Double . The form I get the numbers is like this 8.653762536765E28.
What I want to do is just display the first 2 decimal digits. I want it like 8.65E28.
I tried to find about formatting double values but I wan't able to do it. The result I was getting was 86537...12312.00 .
What do you thing is a good approach for this case? How can I manage only the digits in front of the E (the base) and not the whole number?
I think this is what you want to achieve:
double d = 8.653762536765E28;
DecimalFormat df = new DecimalFormat("0.00E0");
System.out.print(df.format(d));

Rounding to a fixed relative precision [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Round a double to 3 significant figures
(7 answers)
Closed 12 months ago.
I get number representing strings like
248.03500000066338
313.44999999979470
4.2346999999
and I need to round them to something like
248.035
313.45
4.2347
while keeping a fixed relative precision of let's say 6 significant figures. I could do it using Math.log10, computing what absolute precision is needed, and rounding correspondingly, but I wonder if there's a simple way.
Ideally, the resulting number should be such that it does not produce the trailing nines when converted to string, but this is not needed and maybe impossible.
It might not be the most performant solution but I think this is the easiest one:
BigDecimal input = new BigDecimal("248.03500000066338");
double rounded = input.round(new MathContext(6)).doubleValue();

Categories