Results 1 to 4 of 4

Thread: [2.0] how decimal work?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    [2.0] how decimal work?

    using this code; why doesn the ave is still a whole number?

    int nmb1;
    int nmb2;
    int nmb3;
    decimal ave;

    nmb1 = int.Parse(textBox1.Text);
    nmb2 = int.Parse(textBox2.Text);
    nmb3 = int.Parse(textBox3.Text);

    ave = (nmb1 + nmb2 + nmb3) / 3;

    textBox4.Text = ave.ToString();

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

    Re: [2.0] how decimal work?

    In C-based languages the result of dividing an integer by an integer is always an integer. If you want the result to be a decimal then at least one of the numbers must be a decimal. Change this:
    C# Code:
    1. ave = (nmb1 + nmb2 + nmb3) / 3;
    to this:
    C# Code:
    1. ave = (nmb1 + nmb2 + nmb3) / 3M;
    and you'll get the result you want. Note that the "M" suffix is short for "money" and forces the preceding number to type decimal. Alternatively you could do this:
    C# Code:
    1. ave = Convert.ToDecimal(nmb1 + nmb2 + nmb3) / 3;
    or this:
    C# Code:
    1. ave = (nmb1 + nmb2 + nmb3) / (decimal)3;
    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
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] how decimal work?

    or just:
    Code:
    ave = (nmb1 + nmb2 + nmb3) / 3.0;
    Note that it would be more elegant to use an array, as then you can handle an indefinite number of values.

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

    Re: [2.0] how decimal work?

    Quote Originally Posted by penagate
    or just:
    Code:
    ave = (nmb1 + nmb2 + nmb3) / 3.0;
    Note that it would be more elegant to use an array, as then you can handle an indefinite number of values.
    That will actually not work because 3.0 is type double so that expression will actually return a double rather than a decimal. As such you'd have to convert the result before you could assign it to a decimal variable. If you were assigning to a double variable it would be fine and proper.

    I guess the problem here is also that what's being calculated is an average and in many cases the number on the bottom will be an integer variable. In that case it would be most correct to convert the number on the top to the type you want the result to be.
    Last edited by jmcilhinney; Jun 19th, 2007 at 06:54 PM.
    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

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