Convert Number in Java [duplicate] - java

This question already has answers here:
java how to make user friendly percentage output from float number
(9 answers)
Closed 3 years ago.
How to round a number in Java?
Input: 0.655308; Expected output: 65.53.
Input: 1.0583104; Expected output: 105.83.
In power builder compute expression I use
act_qty *work_hour /
if (on_hour < work_hour ) /
sec_setm_gole_qty ,4)
How to run it in Java?

I would just multiply the value with 100
double roundedValue = value * 100;
System.out.printf("%.2f", roundedValue);

String.format("%.2f%%",value*100)?

Related

Why am I not getting expected output when using String.format() function? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
Why am I not getting expected output when using the String.format() function? The answer should be 123456789.123456789 right?
main()
{
float a = 123456789.123456789f;
System.out.println(String.format("%f", a)); // Actual Output: 123456792.000000
System.out.println(String.format("%.4f", a)); // Actual Output: 123456792.0000
System.out.println(String.format("%.4f", a)); // Actual Output: 123456792.0000
}
Float is 32bit. You just overflowed it and it was added to the integer part of number

Java: How do I round a value that is between 0 and 1 to at least 3 decimal places? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 2 years ago.
I'm trying to get a number when I divide 2 numbers together:
int wins = 3070;
int n = 10000;
double probability = wins/n;
System.out.println(probability);
All it prints is: 0.0
But I'm expecting it to print: 0.307
Atleast one of the values (numerator or denominator) should be type casted with double. Integer divided by integer would result integer. If one of them would be double then result would be upcasted to double. Try it! It should work!

Java Operator Precedence issue for a simple equation [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);
If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.

How to get a int from a long double in java? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.

very simple: 1/20 returns 0.0 [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 7 years ago.
In Processing I have this code:
int numberOfSteps = 20;
float numberOfStepsCalculated = (1/20);
println(numberOfStepsCalculated);
But my print keeps returning 0.0
I can not figure out why!
Thanks for my dumb question
Integer divison.
float numberOfStepsCalculated = (1.0f / numberOfSteps); // 20
When you divide two int(s) the result is an int (or 0). You then widen the result to a float (or 0.0). Promote one of the values in the calculation to a float and you'll get a float result.

Categories