This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 7 years ago.
The title says it all. Right now if I input a number like 100.50, in my program it prints as 100.5. Is there an easy way to make the program recognize the zero?
You can do this trick.
String s = String.format("%.2f", 100.50);
Related
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?
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.
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
This question already has answers here:
How to convert an Int to a String of a given length with leading zeros to align?
(8 answers)
How can I pad a String in Java?
(32 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have a number, I want to pad this number with zero's in the beginning if the digits are less that 9 digits.
currently if I have a number lets say:
val num = "123"
if I use padTo(9,"0") I will get "123000000" but I want "000000123"...
what is the best solution for this?
would be better to get the solution in scala
thanks
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.