?
Solved

Mathematical Operation on Int type

Posted on 2003-03-27
9
Medium Priority
?
173 Views
Last Modified: 2010-05-18
hi, see this.

I have this,

**********

out.println(po_total);
out.println(balance_total);
out.println(in_total);

int a =0;
a = balance_total/po_total;
out.println(a);

*********

All the varible here is declare as int. I can see the output of the po_total, balance_total, and also in_total.

but when i want to calculate % of the certain value, oppss, sorry. Actually the main purpose for me to code this is to find out the percentage of the value. The formula is balance_total/po_total*100. And i want to do it step by step.

So, the output of a is "0". ???

Yes, maybe it's because of int right?, then I have to change the a to double using this code,

double a =0.00;
a = balance_total/po_total;
out.println(a)

ha.. this time it's give me "0.0"

what's wrong??
 
0
Comment
Question by:fks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:thanassis
ID: 8217217
a is double. but balance_total & po_total are integers!

For java this is a problem!

So you have to declare both balance_total & po_total as doubles!
0
 
LVL 3

Expert Comment

by:paskal
ID: 8217224
Hi,

Your problem is that balance_total and po_total both are ints. That makes that the result of balance_total/po_total is an int. You point to this int with a double reference named 'a' but that doesn't change the value you're referring to.
So you have to make at least one of both numbers a double or at least cast it to a double like:

double a =0.00;
a = (double )balance_total/po_total;

or
double a =0.00;
a = balance_total/(double )po_total;

Succes!



0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8218902
if you need percentage, just do:

double a = balance_total * 100.0 / po_total;

100.0 make it a double constant (because of .0), then java will promote int to double and calculate the result.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fks
ID: 8222054
ken, if my

balance_total=16
po_total=44

and a = 16*100.0/44
      = 36.36363636363636
??Ha...

I thouhgt i have declare in 100.0 then what happend.

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8222081
>> I thouhgt i have declare in 100.0 then what happend.
i don't understand you. isn't that you want to get percentage, 16 over 44 is 36.36363636363636%, which is correct.
0
 

Author Comment

by:fks
ID: 8223199
ken, if my

balance_total=16
po_total=44

and a = 16*100.0/44
      = 36.36363636363636
??Ha...

I thouhgt i have declare in 100.0 then what happend.

0
 

Author Comment

by:fks
ID: 8223201
ken, I prefer 36.4% instead of 36.36363636

and i have declare on decimal point only riught?

Do I need to use the DecimalFormat to format it?
0
 
LVL 4

Expert Comment

by:thanassis
ID: 8223251
Yes you have to use Desimal format, like this:

import java.text.*;

DecimalFormat df = new DecimalFormat("#,##0.0;(#,##0.0)");
double a = balance_total*100.0/po_total;
out.println("a="+df.format(a));
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 500 total points
ID: 8225484
>> and i have declare on decimal point only riught?
you didn't declear anything. when you say 100.0 all it means it is a double instead of int. there is not diff between 100.0 and 100.00 and 100.00000000000.

>> Do I need to use the DecimalFormat to format it?
yes, and you do it only when you need to display it. a double value has no format! it only contains value information. formating only make sense when you want to display it or convert it to a String (String will be eventurally used for display sooner or later)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
New style of hardware planning for Microsoft Exchange server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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