Expected Double value to display two zeros after precision value [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 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)

Related

Rounding off issue clarification in java [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I am using Math.round(). This is the sample code:
double value = 0.14499999970197677;
value = Math.round( value* 100.0) / 100.0;
My expectation is it should return 0.15 but it is returning 0.14
Also, if the value is 0.13499999970197677, then it is returning 0.13, why not 0.14
I have already gone through the link round up to 2 decimal places in java?
Please tell me clearly that why this is happening with both the numbers?
In Java, Math.round rounds up/down to closest long value (in mathematics terms, closest whole number).
14.49 will be rounded to 14 (because it is treated as 14.4). It won't take the .09 into account.

How can I display double value in TextView with 15 limit precision? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 5 years ago.
I try this way to display it.
DecimalFormat REAL_FORMATTER = new DecimalFormat("0.0###############");
double value = 691 / 3600000;
textView1.setText(REAL_FORMATTER.format(value));
What's the problem?
All the time TextView shows: 0,0. I thought it should be
0,000191944.
This solve may cause problem with number biger than 10. I need
display double value and I don't know how do it.
When dividing in Java the default value is int. You can try dividing using this way:
double value = 691 /3600000D
double value = (double)691/3600000;
or when divided with two places precision:
double value = 691.0/36.0

Integer vs Double multiplication gives very strange output [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Java double precision sum trouble
(2 answers)
Closed 6 years ago.
When I am trying to multiply 3 (Integer type) with 112.1 (Double) then the result I get contains many decimal points.
Integer a=3;
Double b=112.1;
Double result=a*b = 336.29999999999995
But when we multiply 2 (Integer) with 112.1 (Double) then decimal point is so minimal.
Integer a=2;
Double b=112.1;
Double result=a*b = 224.2
Can anybody give me the reason why this strange behavior comes?
You can't store actual decimal representation of fractions.
This is because computer use binary to save any value, including fractional values.
Therefore, there is some difference between actually saved one and what you want to store.

how to round off digit to 7 decimal place in java [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 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));

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

Categories