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. Floating Value by Dividing in a DataGridView !

Floating Value by Dividing in a DataGridView !

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 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.
  • N Offline
    N Offline
    nassimnastaran
    wrote on last edited by
    #1

    Hi All ! in a DataGridView , for example :

    DataGridView1[0,0].Value=1.2*3;

    Result is : 3.6 But if :

    DataGridView1[0,0].Value=2/3

    Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks

    P W C 3 Replies Last reply
    0
    • N nassimnastaran

      Hi All ! in a DataGridView , for example :

      DataGridView1[0,0].Value=1.2*3;

      Result is : 3.6 But if :

      DataGridView1[0,0].Value=2/3

      Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks

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

      The result is 0 because you are dividing an integer by an integer, which leads the compiler to treat the result as an integer. The trick is to cast the first number to a double and then base the calculation on this.

      Forgive your enemies - it messes with their heads

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      N 1 Reply Last reply
      0
      • N nassimnastaran

        Hi All ! in a DataGridView , for example :

        DataGridView1[0,0].Value=1.2*3;

        Result is : 3.6 But if :

        DataGridView1[0,0].Value=2/3

        Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-

        DataGridView1[0,0].Value=2f/3;

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        N L 2 Replies Last reply
        0
        • P Pete OHanlon

          The result is 0 because you are dividing an integer by an integer, which leads the compiler to treat the result as an integer. The trick is to cast the first number to a double and then base the calculation on this.

          Forgive your enemies - it messes with their heads

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          N Offline
          N Offline
          nassimnastaran
          wrote on last edited by
          #4

          Yes , It's a Big Mistake for Me ! Thanks !

          1 Reply Last reply
          0
          • W Wayne Gaylard

            by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-

            DataGridView1[0,0].Value=2f/3;

            When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

            N Offline
            N Offline
            nassimnastaran
            wrote on last edited by
            #5

            :doh: Thanks!

            1 Reply Last reply
            0
            • W Wayne Gaylard

              by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-

              DataGridView1[0,0].Value=2f/3;

              When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Wayne Gaylard wrote:

              by writing it 2/3 the compiler thinks you are doing integer division

              FTFY :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              W 1 Reply Last reply
              0
              • L Luc Pattyn

                Wayne Gaylard wrote:

                by writing it 2/3 the compiler thinks you are doing integer division

                FTFY :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                W Offline
                W Offline
                Wayne Gaylard
                wrote on last edited by
                #7

                I was giving him(OP) the benefit of the doubt :)

                When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                1 Reply Last reply
                0
                • N nassimnastaran

                  Hi All ! in a DataGridView , for example :

                  DataGridView1[0,0].Value=1.2*3;

                  Result is : 3.6 But if :

                  DataGridView1[0,0].Value=2/3

                  Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks

                  C Offline
                  C Offline
                  CodeChandra
                  wrote on last edited by
                  #8

                  That is Modular division. if it is like 4/3 ans will be 1. U can get the ans if u make it like 2.0/3 or 2/3.0

                  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