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 values issue

floating values issue

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 6 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.
  • R Offline
    R Offline
    Ryno Burger
    wrote on last edited by
    #1

    Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

    J L L B E 5 Replies Last reply
    0
    • R Ryno Burger

      Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      You need to cast your int values to float values before you divide them.

      1 Reply Last reply
      0
      • R Ryno Burger

        Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; overallTotalAmps = (float)overTotalWatt / (float)voltage;

        led mike

        R 1 Reply Last reply
        0
        • L led mike

          float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; overallTotalAmps = (float)overTotalWatt / (float)voltage;

          led mike

          R Offline
          R Offline
          Ryno Burger
          wrote on last edited by
          #4

          Great stuff!! Thanks to both of you that replied. It works perfectly!!! :-D

          1 Reply Last reply
          0
          • R Ryno Burger

            Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

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

            Hi, adding to the other replies: many problem domains are continuous by nature; if so, you should not use integers at all. The simplest approach is to make overallTotalAmps , voltage, overTotalWatt, ... all float or double. That way you don't have to cast, and won't have any rounding errors that may go unnoticed. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            1 Reply Last reply
            0
            • R Ryno Burger

              Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

              B Offline
              B Offline
              benjymous
              wrote on last edited by
              #6

              I know you've already got an answer, but I was a bit baffled by the use of ToString and float.parse so I thought I'd give you a rundown of what that actually does overallTotalWatt / voltage => (int)126 / (int)230 => 0 (since we're dividing ints) 0.ToString() => "0" float.Parse("0") => 0.00

              -- Help me! I'm turning into a grapefruit! Buzzwords!

              1 Reply Last reply
              0
              • R Ryno Burger

                Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared: float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; on which I do the following calcluation: overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString()); The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                Anyone that ToString's a number to parse should be dragged outside and shot.

                Need a C# Consultant? I'm available.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                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