Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Division problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon - 13 Oct 2007 19:12 GMT
Hello all,

I'm trying to work out why the following always returns 0:

ulong b = (750 / 1128);

I've tried other types, not just ulong but always with the same
results, 0;

Am I missing something? It been a long day!

Thanks

Jon
Phil H - 13 Oct 2007 20:06 GMT
> Hello all,
>
[quoted text clipped - 10 lines]
>
> Jon

John

ulong is an integer data type (unsigned 64 bit) which cannot represent
the fraction 750/1120

If you are working with non-integral numbers you need to work with
types such as float, single, double etc.

However, one word of warning. Even when an expression is being
assigned to a non-integral numeric type the result is evaluated is
though it is an integer if all the terms themselves are integers. The
expression (750 / 1128) will be treated as an integer because 750,
1128 are themselves integers, and since 0 < (750/1120) < 1 it is
rounded down to 0

Try this instead:

float b = 750.0F / 1128.0F

I guarantee it will not be zero.
Alexey Smirnov - 13 Oct 2007 20:54 GMT
> Hello all,
>
[quoted text clipped - 10 lines]
>
> Jon

ulong is an integer and 750 / 1128 equals 0.66 - the integer part is 0

If you need to round it up, use Math.Round, for example

ulong b = Convert.ToUInt64(Math.Round(750D / 1128D)); // =1 :-)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.