Solved

Printing 12/110*100 always returning 0.0

Posted on 2012-04-02
3
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

864 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