|
-
Aug 29th, 2005, 11:44 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Double issues
I am pulling my hair out with this. I have VB.Net code that works, but the C# alternative does not.
VB.Net
Dim MyVal as Double = 500 / 792
'MyVal then equals 0.631313131313131
C#
double MyVal = 500 / 792;
//MyVal then equals 0.0
How can I get the C# code to give me the same result as the VB.Net?
Last edited by blindlizard; Aug 30th, 2005 at 03:32 AM.
-
Aug 30th, 2005, 12:39 AM
#2
Re: Double issues
VB.NET allows more implicit conversions than C#. You are dividing an integer by an integer, which in C# will always produce an integer. You should find that:
Code:
double myVal = 500.0 / 792.0
will give you the correct value. You could also use:
Code:
double myVal = (double)500 / (double)792
Edit:
To clarify, it is the fact that you are dividing by an integer that is the issue. You can divide an integer by a double and get a double no problem, e.g. 500 / 792.0. I mispoke a little with regard to VB.NET. Implicit conversion is not the issue here, but rather what constitutes integer division. As I said, in C# (as for C and all its children) dividing by an integer returns an integer. In VB.NET, any division returns a floating-point number (usually Double but can be Single or Decimal in special cases) unless you specify integer division, which you do by using "\" instead of "/".
Last edited by jmcilhinney; Aug 30th, 2005 at 12:51 AM.
-
Aug 30th, 2005, 03:31 AM
#3
Thread Starter
Frenzied Member
Re: Double issues
Ah, thank you! I did try (double)(500 / 760) but that was the same answer (0.0) probably because by the time I tell it to give me a double, it is already an integer.
-
Aug 30th, 2005, 03:35 AM
#4
Re: [RESOLVED] Double issues
Yes, that is correct. You have to convert them before you do the devision.
- ØØ -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|