Solved

Printing 12/110*100 always returning 0.0

Posted on 2012-04-02
3
237 Views
Last Modified: 2012-04-12
int x = 12
int y =110

I need to print this either as double or float. It always shows as 0.0
float percentage =(float)(x/y);
0
Comment
Question by:newone2011
3 Comments
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 250 total points
ID: 37796312
Try:

float percentage = (float)x / y;
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 250 total points
ID: 37799290
Since division of two integers (i.e. both nominator and denominator are integers) will only return integer value. Therefore, you may use either wdosanjos's comments or the following:

double percentage = x*1.0/y;
0
 

Author Closing Comment

by:newone2011
ID: 37840075
Good
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now