Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Float Data Type

Float Data Type

Scheduled Pinned Locked Moved C#
questioncsharpdata-structureshelptutorial
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Imran Adam
    wrote on last edited by
    #1

    ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???

    Cheers :)

    A D P 3 Replies Last reply
    0
    • I Imran Adam

      ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???

      Cheers :)

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Try this:

      float result = (Total*1.0)/(NumberArray.Length*1.0);
      .
      .
      Console.WriteLine("Average " + result);

      If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"

      There are 10 kinds of people: those who understand binary and those who don't

      D I 2 Replies Last reply
      0
      • I Imran Adam

        ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???

        Cheers :)

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #3

        I assume total is also an integer. IF so what's happening is that you're doing integer division, getting an integer result and then converting the int into a float. What you need to do is cast one of the ints into a float before doing the division. float result = (float)Total/NumberArray.Length; Depending on order of operations you might need a second parenthesis to get the desired result. float result = ((float)Total)/NumberArray.Length;

        -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

        I 1 Reply Last reply
        0
        • A Andrei Ungureanu

          Try this:

          float result = (Total*1.0)/(NumberArray.Length*1.0);
          .
          .
          Console.WriteLine("Average " + result);

          If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"

          There are 10 kinds of people: those who understand binary and those who don't

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          a real cast is more efficient than your kludge. In any event you don't need to cast both numbers, just the one.

          -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

          1 Reply Last reply
          0
          • A Andrei Ungureanu

            Try this:

            float result = (Total*1.0)/(NumberArray.Length*1.0);
            .
            .
            Console.WriteLine("Average " + result);

            If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"

            There are 10 kinds of people: those who understand binary and those who don't

            I Offline
            I Offline
            Imran Adam
            wrote on last edited by
            #5

            Thanks Andrei How silly of me, i should of thought of type 'casting';) this also works float result = ((float)Total/NumberArray.Length);

            Cheers :)

            A 1 Reply Last reply
            0
            • D Dan Neely

              I assume total is also an integer. IF so what's happening is that you're doing integer division, getting an integer result and then converting the int into a float. What you need to do is cast one of the ints into a float before doing the division. float result = (float)Total/NumberArray.Length; Depending on order of operations you might need a second parenthesis to get the desired result. float result = ((float)Total)/NumberArray.Length;

              -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

              I Offline
              I Offline
              Imran Adam
              wrote on last edited by
              #6

              Yes thanks Dan ;)

              Cheers :)

              A 1 Reply Last reply
              0
              • I Imran Adam

                Thanks Andrei How silly of me, i should of thought of type 'casting';) this also works float result = ((float)Total/NumberArray.Length);

                Cheers :)

                A Offline
                A Offline
                Andrei Ungureanu
                wrote on last edited by
                #7

                I know that casting also works, it was just another way of doing things ;)

                There are 10 kinds of people: those who understand binary and those who don't

                1 Reply Last reply
                0
                • I Imran Adam

                  Yes thanks Dan ;)

                  Cheers :)

                  A Offline
                  A Offline
                  Anthony Mushrow
                  wrote on last edited by
                  #8

                  Also, you may want to limit the number of decimal places shown, you can do that like this: float fTest = 123.456789f; string sTest = fTest.ToString("N3"); that would put it at 3 decimal places, N5 would show 5 decimal places and so on. Just thought you might like to know. Incase you didn't already.

                  My current favourite word is: Waffle Cheese is still good though.

                  1 Reply Last reply
                  0
                  • I Imran Adam

                    ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???

                    Cheers :)

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    Ooh - you're hitting an issue that regularly causes confusion and gets a lot of people hot under the collar about .NET. Basically, the issue is that the Total value is an integer. You need to cast the result to a float so this would become float result = (float)Total/NumberArray.Length;

                    Deja View - the feeling that you've seen this post before.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups