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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. An easy one... problems converting a decimal to an integer

An easy one... problems converting a decimal to an integer

Scheduled Pinned Locked Moved C#
10 Posts 4 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.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, I've got an easy on for you all. Since I chose to have input of QueryTime possibly be a decimal (ex ".5"), I have kept it a decimal. However, Timer.Interval is an int and I need to convert a decimal to an int. public void StartTimer(decimal QueryTime) // decimal QueryTime = 0.5 { int shizz = Decimal.ToInt32(QueryTime); // int shizz = 0 int wtf = (Decimal.ToInt32(QueryTime) * 60) * 1000; // int wtf = 0 QueryTimer.Interval = ((int)QueryTime * 60)* 1000; QueryTimer.Enabled = true; } Event when QueryTime = 0.5, both shizz and wtf are 0. Does anyone know how to properly convert a decimal to an integer? Thanks, Matt

    D L L 3 Replies Last reply
    0
    • B bbranded

      Hello, I've got an easy on for you all. Since I chose to have input of QueryTime possibly be a decimal (ex ".5"), I have kept it a decimal. However, Timer.Interval is an int and I need to convert a decimal to an int. public void StartTimer(decimal QueryTime) // decimal QueryTime = 0.5 { int shizz = Decimal.ToInt32(QueryTime); // int shizz = 0 int wtf = (Decimal.ToInt32(QueryTime) * 60) * 1000; // int wtf = 0 QueryTimer.Interval = ((int)QueryTime * 60)* 1000; QueryTimer.Enabled = true; } Event when QueryTime = 0.5, both shizz and wtf are 0. Does anyone know how to properly convert a decimal to an integer? Thanks, Matt

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      A decimal will not fit in an integer so you have to decide in code how you want it to fit. Have a look at the Math class in particular its Round method with MidPointRoundingMode

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

      B 1 Reply Last reply
      0
      • D DaveyM69

        A decimal will not fit in an integer so you have to decide in code how you want it to fit. Have a look at the Math class in particular its Round method with MidPointRoundingMode

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        B Offline
        B Offline
        bbranded
        wrote on last edited by
        #3

        Hello! Thanks for your response. I am giving the users the option to set the interval of the timer. I had specified "minutes." Instead, I can specify "seconds" and keep it int throughout. However, I'll take a look at this for future reference. Thanks! Matt Brown

        D 1 Reply Last reply
        0
        • B bbranded

          Hello! Thanks for your response. I am giving the users the option to set the interval of the timer. I had specified "minutes." Instead, I can specify "seconds" and keep it int throughout. However, I'll take a look at this for future reference. Thanks! Matt Brown

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          In that case, maybe a TimeSpan would be more appropriate? I used some thing similar in my ApplicationIdle component article[^]

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          1 Reply Last reply
          0
          • B bbranded

            Hello, I've got an easy on for you all. Since I chose to have input of QueryTime possibly be a decimal (ex ".5"), I have kept it a decimal. However, Timer.Interval is an int and I need to convert a decimal to an int. public void StartTimer(decimal QueryTime) // decimal QueryTime = 0.5 { int shizz = Decimal.ToInt32(QueryTime); // int shizz = 0 int wtf = (Decimal.ToInt32(QueryTime) * 60) * 1000; // int wtf = 0 QueryTimer.Interval = ((int)QueryTime * 60)* 1000; QueryTimer.Enabled = true; } Event when QueryTime = 0.5, both shizz and wtf are 0. Does anyone know how to properly convert a decimal to an integer? Thanks, Matt

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Other idea: int non_wtf = Decimal.ToInt32(QueryTime * 60 * 1000); Btw, why a decimal? Why not, say, a float or double? Almost everything that uses non-integral values uses either floats or doubles, decimal is rare (and slow, and only meant for when the numbers are for human interaction since they expect 4 instead of 3.99999999 for example)

            B 1 Reply Last reply
            0
            • L Lost User

              Other idea: int non_wtf = Decimal.ToInt32(QueryTime * 60 * 1000); Btw, why a decimal? Why not, say, a float or double? Almost everything that uses non-integral values uses either floats or doubles, decimal is rare (and slow, and only meant for when the numbers are for human interaction since they expect 4 instead of 3.99999999 for example)

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

              Thanks for the input. I'd like to have users be able to input values of 10-.01 as a string. I then would like to convert this string to a value. I would then multiply this value by 60, then 1000. Then I would like to take this value and change it into an integer. What value should I be converting the string to perform the calculations? Thanks, Matt

              L 1 Reply Last reply
              0
              • B bbranded

                Thanks for the input. I'd like to have users be able to input values of 10-.01 as a string. I then would like to convert this string to a value. I would then multiply this value by 60, then 1000. Then I would like to take this value and change it into an integer. What value should I be converting the string to perform the calculations? Thanks, Matt

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Small tip, multiply by 60000 instead, bit faster. Since you're turning it into an int anyway, and considering the range of expected values, you could use a float without trouble (several orders of magnitude faster than a decimal) Something like:

                int someFunctionName(string input)
                {
                return (int)(float.Parse(input) * 60000);
                }

                warning: untested In RealLife(tm) you might want to use float.TryParse and do something clever in case it returns false.

                L 1 Reply Last reply
                0
                • L Lost User

                  Small tip, multiply by 60000 instead, bit faster. Since you're turning it into an int anyway, and considering the range of expected values, you could use a float without trouble (several orders of magnitude faster than a decimal) Something like:

                  int someFunctionName(string input)
                  {
                  return (int)(float.Parse(input) * 60000);
                  }

                  warning: untested In RealLife(tm) you might want to use float.TryParse and do something clever in case it returns false.

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

                  harold aptroot wrote:

                  multiply by 60000 instead, bit faster.

                  Actually QueryTime * 60 * 1000 is OK, the compiler optimizes that to *60000 while the reader can better see the intent. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  L 1 Reply Last reply
                  0
                  • B bbranded

                    Hello, I've got an easy on for you all. Since I chose to have input of QueryTime possibly be a decimal (ex ".5"), I have kept it a decimal. However, Timer.Interval is an int and I need to convert a decimal to an int. public void StartTimer(decimal QueryTime) // decimal QueryTime = 0.5 { int shizz = Decimal.ToInt32(QueryTime); // int shizz = 0 int wtf = (Decimal.ToInt32(QueryTime) * 60) * 1000; // int wtf = 0 QueryTimer.Interval = ((int)QueryTime * 60)* 1000; QueryTimer.Enabled = true; } Event when QueryTime = 0.5, both shizz and wtf are 0. Does anyone know how to properly convert a decimal to an integer? Thanks, Matt

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

                    Hi Matt, Everything you tried there, including ((int)QueryTime * 60)* 1000; is wrong; the heart of the problem is (int)x where x is float/double/decimal is rounding towards zero, so if x is between -1 and 1, it will be rounded to zero. You should perform the entire calculation in the highest precision that is present, and only then convert it to an integer. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      harold aptroot wrote:

                      multiply by 60000 instead, bit faster.

                      Actually QueryTime * 60 * 1000 is OK, the compiler optimizes that to *60000 while the reader can better see the intent. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      - 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Depends on how you write it of course, exactly like "QueryTime * 60 * 1000" it would be ok but most other ways to write it wouldn't be

                      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