473,490 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculating a percent in a c# web app

I can not figure out what I'm doing wrong. I'm trying to calc a percent by
dividing a small number by a large number, but result always = 0.

This is what I have.

double nbr1
double nbr2
int pct

nbr1 = 180
nbr2 = 500

pct = (nbr1 / nbr2) * 100

The pct always = 0 and it should be 36. Can anyone tell me what I'm doing
wrong.

Thanks in advance.

--

Jul 21 '05 #1
4 1192
Hi,

The code you presented will not compile, if you fix the compile errors I am
100% you will get the expected results.

double nbr1;
double nbr2;
int pct;

nbr1 = 180;
nbr2 = 500;

pct = (int)((nbr1 / nbr2) * 100);

I am going to go out on a limb here and guess that you missed the extra
parens when casting, for instance
pct = (int)(nbr1 / nbr2) * 100; // Note the missing parens here

This will result in an answer of 0 since the division is first cast to
integer effectively truncating 0.36 to 0 and then multiplied by 100.

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

"DanP" <Da**@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
I can not figure out what I'm doing wrong. I'm trying to calc a percent by
dividing a small number by a large number, but result always = 0.

This is what I have.

double nbr1
double nbr2
int pct

nbr1 = 180
nbr2 = 500

pct = (nbr1 / nbr2) * 100

The pct always = 0 and it should be 36. Can anyone tell me what I'm doing
wrong.

Thanks in advance.

--

Jul 21 '05 #2
Hi,

The code you presented will not compile, if you fix the compile errors I am
100% you will get the expected results.

double nbr1;
double nbr2;
int pct;

nbr1 = 180;
nbr2 = 500;

pct = (int)((nbr1 / nbr2) * 100);

I am going to go out on a limb here and guess that you missed the extra
parens when casting, for instance
pct = (int)(nbr1 / nbr2) * 100; // Note the missing parens here

This will result in an answer of 0 since the division is first cast to
integer effectively truncating 0.36 to 0 and then multiplied by 100.

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

"DanP" <Da**@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
I can not figure out what I'm doing wrong. I'm trying to calc a percent by
dividing a small number by a large number, but result always = 0.

This is what I have.

double nbr1
double nbr2
int pct

nbr1 = 180
nbr2 = 500

pct = (nbr1 / nbr2) * 100

The pct always = 0 and it should be 36. Can anyone tell me what I'm doing
wrong.

Thanks in advance.

--

Jul 21 '05 #3
I think if you make the change below it should work.

pct = (int)((nbr1 / nbr2) * 100.0);

"DanP" wrote:
I can not figure out what I'm doing wrong. I'm trying to calc a percent by
dividing a small number by a large number, but result always = 0.

This is what I have.

double nbr1
double nbr2
int pct

nbr1 = 180
nbr2 = 500

pct = (nbr1 / nbr2) * 100

The pct always = 0 and it should be 36. Can anyone tell me what I'm doing
wrong.

Thanks in advance.

--

Jul 21 '05 #4
I think if you make the change below it should work.

pct = (int)((nbr1 / nbr2) * 100.0);

"DanP" wrote:
I can not figure out what I'm doing wrong. I'm trying to calc a percent by
dividing a small number by a large number, but result always = 0.

This is what I have.

double nbr1
double nbr2
int pct

nbr1 = 180
nbr2 = 500

pct = (nbr1 / nbr2) * 100

The pct always = 0 and it should be 36. Can anyone tell me what I'm doing
wrong.

Thanks in advance.

--

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6577
by: Lars G. Svensson | last post by:
As a regular ciwas reader, I learned that fixed-pixel designs are evil, so for the newly created site http://www.ddc-deutsch.de (in German, still very much under construction) I arranged the...
2
35858
by: mhodkin | last post by:
I created a query in which I have grouped data by City. I wish to calculate the percent of each value, e.g. City/(Total count of all Cities), in tbe next column of the query. I can't seem to...
5
9402
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
4
4829
by: zoeb | last post by:
Hi. I have a form which the user enters 2 years worth of data into (one record per year). The aim, is to populate the table this form is based on with 3 more years worth of data (i.e. creating 3...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.