Mathematical Operations in Java

0 votes

This is my code :

double width = 50/110000;
System.out.println("width ori is "+width );

And the output is: 0.00000000000

What's wrong ? the expected output has to be 4.5454545454545455E-4

Any body can explain to me why?

Feb 28, 2019 in Java by Sushmita
• 6,920 points
654 views

1 answer to this question.

0 votes

Ok. This is happening because you're dividing two integers, so it will only take the integer part (integer division).

Dividing integers in a computer program requires special care. Some programming languages, treat integer division (i.e by giving the integer quotient as the answer). So the answer is an integer.

Examples :

In real life                  In Java

4/3  = 1.33333                4/3  = 1
25/12 = 2.083333              25/12 = 2
9/2 = 4.5                     9/2 = 4
50/110000 = 0.000454545       50/110000 = 0

You can cast one of the number (or both but it's actually useless) to double to avoid that :

double width = (double)50/110000;
double width = 50d/110000;
double width = 50.0/110000;
answered Feb 28, 2019 by developer_1
• 3,350 points

Related Questions In Java

+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,222 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,576 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,310 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,140 points
2,548 views
0 votes
2 answers

“Could not find or load main class” mean?

Use the final modifier to enforce good initialization. Avoid returning ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,920 points
4,515 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,103 views
0 votes
2 answers

How do I replace character from string at specific indexes?

You could turn the String into a ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
23,772 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
16,066 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,699 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,190 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP