How do I print the remainng text? [duplicate] - java

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

Related

Java BigDecimal divide and get digit to N places [duplicate]

This question already has answers here:
BigDecimal setScale and round
(2 answers)
Set specific precision of a BigDecimal
(5 answers)
Closed 1 year ago.
I am trying to write a program that has a step in it where I would like to divide two numbers and get a decimal number to 60 places.
For instance, I would like to divide 1 by 17 and get 0.016393442622950819672131147540983606557377049180327868852459 without losing any precision.
I am trying to store the number in a BigDecimal but I am having trouble finding a good way to accomplish this without losing the precision after the 16th digit or so

Round answer based on what the user inputs? [duplicate]

This question already has answers here:
How to Java String.format with a variable precision?
(4 answers)
Java printf using variable field size?
(4 answers)
Closed 2 years ago.
I'm a new programmer, and I'm coding a basic decimal calculator. (in Java)
If the user inputs how many decimals they want to round to, say 2, how would I incorporate that into the code, and have the answer round to 2 decimals?
Right now, I have something like this:
System.out.printf("%.3f %n", ans);
But that will always round to 3 decimals, no matter what the user input is.
Here's a link to the whole code, if necessary.
Is there a basic way to this?

How to round to 1 decimal place using Java [duplicate]

This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.

Double/Float output issues in Java Android [duplicate]

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)

java rounding numbers [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 9 years ago.
I retrieve some data from a database but the number has a lot of digits and I want to round it. The thing is I do not want the whole number to be rounded.
Example :
I draw number 4.1010359882326385E10 which is of type long.
I want to round it to 4.10103598823264.
That is to round the number after the 15th digit.
Any way I can do this?
E10 in your number is a scientific notation, that says you number is actually 41010359882.326385. If you transform it to 4.10103598823264, you are not doing a rounding, you are doing something else.

Categories

Resources