Results 1 to 4 of 4

Thread: [RESOLVED] Double issues

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Resolved [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.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    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.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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
  •  



Click Here to Expand Forum to Full Width