Printing a simple double number in java [duplicate] - java

This question already has answers here:
Dividing error in java language
(2 answers)
Closed 8 years ago.
pls enlighten me why the heck is this not working ? :| it's just a simple output line , god i'm so frustated. I know it might be something very that i miss , but i can't figure it out.
double a = 155/124;
System.out.printf("%f\n", a);
it prints
1.00000

it's working just fine. Just remember 2 things:
you're using integers, the result of the calculation will be an integer
rounding errors

Well, you are dividing two ints without casting it to double explicitly.
Add (double) cast to variable a:
double a = (double) 155/124;
Or, you can create one of the numbers to be double, like:
double a = 155/124.0;
Hope this helps.

try this
double a = 155d / 124d ;
System.out.printf("%f\n", a);

Related

How to get Decimal part in Double variable Division[JAVA] [duplicate]

This question already has answers here:
Beginners Java Question (int, float)
(4 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
If I divide a double type variable, the Decimal part becomes zero.
a=13122/10;
System.out.println (a);
Prints
1312.0
As you can see, the Decimal part became zero when I divided it.
But I need the value
1312.2
Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.
What you need is this,
a = (double)13122/10;
or this:
a = 13122.0/10;
You are dividing integers. You can cast the division and then you are ok
double a = (double) 13122/10;
System.out.println (a);

Can anyone explain how do we get this output ? (bitwise not) [duplicate]

This question already has answers here:
Java: What does ~ mean
(5 answers)
Closed 5 years ago.
int x=10;
System.out.println(~x);
//this will print -11
//how to do the calculation manually using complement arithmetic
This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this
Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;
int x = 10;
System.out.println(Integer.toBinaryString(x)); //00000000000000000000000000001010
System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
System.out.println(~x); //-11

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 double number division giving strange output [duplicate]

This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

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