473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Weird calculation

Gas
I am new to C#, I want to perfoem a calculation and I have a weird out come
from the app.

Here is the code

double percentage = 0;

percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height)

when I try to output the percentage, it is always 0.
(ctrl1.Height and ctrl2.Height are positive value)

anyone can give me some help here?

Nov 16 '05 #1
5 1562
Hi,

You get 0 because you need to cast the result to double (since the
percentage is double).

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Gas" <ga****@hotmail.com> je napisal v sporočilo
news:u4**************@TK2MSFTNGP14.phx.gbl ...
I am new to C#, I want to perfoem a calculation and I have a weird out come
from the app.

Here is the code

double percentage = 0;

percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height)

when I try to output the percentage, it is always 0.
(ctrl1.Height and ctrl2.Height are positive value)

anyone can give me some help here?

Nov 16 '05 #2
Gas schrieb:
percentage = 1.0 * ctrl1.Height / (ctrl1.Height+ ctrl2.Height) ^^^^^ add this
anyone can give me some help here?


The compiler performs the calculation on the right side with integer
arithmetics since all variables are integers. By adding '1.0' you force
the compiler to switch to floating point arithmetics.

Christian
Nov 16 '05 #3
Peter Jausovec schrieb:
You get 0 because you need to cast the result to double


Haven't tried it out, so maybe I'm wrong, but wouldn't that yield the
same result? Don't you instead have to cast only _one_ factor to double
to make the compiler use floating point arithmetic? (double)(1/2)
should still be 0, but (double)1/2 is 0.5. Or am I missing something?

Christian
Nov 16 '05 #4
You are correct. You need to cast one factor in the expression to
(double) in order to force floating point arithmetic. If you cast the
entire expression then the result will be the same.

I would suggest this rather than multiplying by 1.0 for maintenance
reasons. It's easier to understand the intent of

percentage = (double)ctrl1.Height / (ctrl1.Height+ ctrl2.Height)

than it is to understand why someone did this
percentage = 1.0 * ctrl1.Height / (ctrl1.Height+ ctrl2.Height)

Nov 16 '05 #5
Bruce Wood schrieb:
than it is to understand why someone did this
percentage = 1.0 * ctrl1.Height / (ctrl1.Height+ ctrl2.Height)


Well, since I usually use this method I understand perfectly. :) But I
agree with you that explicit casting is much clearer and easier to
understand.

Christian
Nov 16 '05 #6

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

Similar topics

82
by: nobody | last post by:
Howdy, Mike! mikecoxlinux@yahoo.com (Mike Cox) wrote in message news:<3d6111f1.0402271647.c20aea3@posting.google.com>... > I'm a C++ programmer, and have to use lisp because I want to use >...
0
by: anaxamandr | last post by:
Hi. I have a long loop in ASP that performs a rather lengthy calculation. I would love for my users to be able to stop that calculation, if they so choose, mid way through the process. I attempted...
2
by: Del | last post by:
Thanks in advance for any help. I have a database that was created in Access 2000. Several users have been upgraded to Access 2003. Since upgrading to 2003 we have noticed that some of the...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
1
by: Gas | last post by:
I am new to C#, I want to perfoem a calculation and I have a weird out come from the app. Here is the code double percentage = 0; percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height) ...
10
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It...
38
by: baong | last post by:
dear all i have use this line to time in many my web base appl. but now i found a weird problem the javascript line is the same but i use the calculation 2 time @ the first time, it is ok, 3 *...
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
3
by: mattmao | last post by:
Okay, I was asked by a friend about the result of this limit: http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg Not n->zero but n-> + infinite I really...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.