Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Mathematical Operation on Int type

Posted on 2003-03-27
Medium Priority
180 Views
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
Question by:fks
• 3
• 3
• 2
• +1

LVL 4

Expert Comment

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

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

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

Author Comment

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

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

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

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

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

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

Question has a verified solution.

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

Dropbox has a relatively new feature called Smart Sync.  This feature allows Dropbox Professional (not plus) and Dropbox Business (if enabled) users to store information in Dropbox WITHOUT storing any files on their computer.
As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a userâ€™s mailbox on Exchange Server. Though, if the serverâ€™s status is changed or it is damaâ€¦
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calculâ€¦
###### Suggested Courses
Course of the Month15 days, 4 hours left to enroll

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

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