Double/Float output issues in Java Android [duplicate] - java

This question already has answers here:
Java Double to String conversion without formatting
(9 answers)
Closed 9 years ago.
In my application, I allowed user to input DOUBLE value (2 decimal places) then total up and display. It works fine with value lesser than 10,000,000; However, when displaying
Double totalvalue = 1000000000.50;
Displayed as 1.0E9
Intent to get Display as : 1000000000.50
Double totalvalue = 10000000.00
Displayed as 1.0E7
Intent to get Display as : 10000000.00
So my problem is how to get Display the actual value?
p/s: I did research on this issues for few hours but unfortunately I doesn't get any answer for that.

You can use this:
String.format("%1$.2f", totalvalue);
to format your Double without the E notation.

You can display as-
String.format("%.2f", totalValue)

Related

How do I print the remainng text? [duplicate]

This question already has answers here:
System.out.printf vs System.out.format
(4 answers)
Formatting output java
(1 answer)
displaying information using printf in Java
(1 answer)
Java output formatting for Strings
(6 answers)
Closed 19 hours ago.
I finally found out how to print a Double in Java with 2 decimal points. However, I now can't figure out how to keep printing text after the Double is sent out.
I tried to print something like this.
double maxTemp;
maxTemp = 5.43210;
System.out.format("\nThe highest Temperature recorded was: %.2f", maxTemp, "*C");
Then I wanted it to round the temperature to 2 decimal places and display:
The highest Temperature recorded was: 5.43*C
However it keeps cutting off the '*C' after the temperature is displayed.
This is the output:
The highest Temperature recorded was: 5.43
How can I fix this?
Put the "*C" in the format string, not as an additional argument.
System.out.format("%nThe highest Temperature recorded was: %.2f*C", maxTemp);
Alternatively, you can use %s for a string placeholder.
System.out.format("%nThe highest Temperature recorded was: %.2f%s", maxTemp, "*C");

java-Not getting the result with decimal places [duplicate]

This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing simple calculation in java. Expected result is 51.3348 but what I am getting is 51.0, here is my calculation
float percent = (7819140000l-3805200000l)*100/7819140000l;
Is that problem with datatype? How can I resolve this to get value as 51.3348
Thanks in Advance
add an f to one of the values:
float percent = (7819140000l-3805200000l)*100f/7819140000l;
if yiu do not do it, Java will make a devision by long values

Properly converting a double variable to string [duplicate]

This question already has answers here:
Converting double to string
(17 answers)
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 6 years ago.
I am trying to take a Double variable called 'startCheckNumber' which should have a value of '40305555' and convert to String. In doing a debug of my code the startCheckNumber shows a value of 4.030555E7. If I do the following command to convert to String it shows it like that instead of '4030555'
String displayCheckNumber = String.valueOf(startCheckNumber) ;
Is there a better way to convert a double variable to String in this case than using ValueOf? I tried 'Double.toString(number)' format and that didn't work right
Thanks
Sorry I should have looked harder. I found and this seemed to work
DecimalFormat decimalFormat = new DecimalFormat("#");
String displayCheckNumber = decimalFormat.format(startCheckNumber);

Converting string to double with dot [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 6 years ago.
I need to convert string value to double with dot. Here is simple code
double dValue=Double.parseDouble("999999999.99");
System.out.println(dValue);
output is: 9.9999999999E8
When i gave value like 10000 or 100000 it works. Help me to overcome this problem.
You could use BigDecimal and toPlainString() for that.
BigDecimal dValue= new BigDecimal("999999999.99");
System.out.println(dValue.toPlainString());
Output:
999999999.99
You can use String.format
System.out.println(String.format("%.2f", dValue));

Java Deduction gives wrong result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
In My Java code I'm trying to do following
double a=1769.58;
double b=986.58;
double c=a-b;
System.out.println("Result "+c);
This is retuning the result as 782.9999999999999. but it should be 783.00 what is wrong with this.how can I get correct value and what is the reason for this?
It's because computer can't stored exactly the good value of floating point.
See this response to learn more about this problem.
try this:
double a=1769.58;
double b=986.58;
int c=(int)a-(int)b;

Categories

Resources