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. Working with Decimals

Working with Decimals

Scheduled Pinned Locked Moved C#
graphicshelpquestion
6 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.
  • J Offline
    J Offline
    Jim Warburton
    wrote on last edited by
    #1

    I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?

    this thing looks like it was written by an epileptic ferret Dave Kreskowiak

    M CPalliniC B 3 Replies Last reply
    0
    • J Jim Warburton

      I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?

      this thing looks like it was written by an epileptic ferret Dave Kreskowiak

      M Offline
      M Offline
      Matthew Butler 0
      wrote on last edited by
      #2

      I'm sure there is an even better way, but... decimal d = 10.1234; int dp = (d - decimal.Truncate(d)).ToString().Length - 2; int val = (int)(d * (decimal)Math.Pow(10, dp)); This method, although still converting it to a string, doesn't 'search' the string for a particular character... just gets its length... which is relatively efficient. As I said... there will be a better way... what it is I cannot immediately say. Hope this helps.

      Matthew Butler

      1 Reply Last reply
      0
      • J Jim Warburton

        I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?

        this thing looks like it was written by an epileptic ferret Dave Kreskowiak

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        For instance

        decimal d = 28.9876M;
        int[] bits = Decimal.GetBits(d);
        int pow = (bits[3] >> 16) & 0xFF;

        At the end pow contains 4, i.e. the number of digits after the decimal point.

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        In testa che avete, signor di Ceprano?

        J 1 Reply Last reply
        0
        • J Jim Warburton

          I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?

          this thing looks like it was written by an epileptic ferret Dave Kreskowiak

          B Offline
          B Offline
          buchstaben
          wrote on last edited by
          #4

          you can use Decimal.GetBits(decimal) as described here: http://msdn.microsoft.com/en-us/library/system.decimal.getbits(VS.71).aspx[^] edit: okay, too late again

          J 1 Reply Last reply
          0
          • B buchstaben

            you can use Decimal.GetBits(decimal) as described here: http://msdn.microsoft.com/en-us/library/system.decimal.getbits(VS.71).aspx[^] edit: okay, too late again

            J Offline
            J Offline
            Jim Warburton
            wrote on last edited by
            #5

            Thanks for the help.

            this thing looks like it was written by an epileptic ferret Dave Kreskowiak

            1 Reply Last reply
            0
            • CPalliniC CPallini

              For instance

              decimal d = 28.9876M;
              int[] bits = Decimal.GetBits(d);
              int pow = (bits[3] >> 16) & 0xFF;

              At the end pow contains 4, i.e. the number of digits after the decimal point.

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              J Offline
              J Offline
              Jim Warburton
              wrote on last edited by
              #6

              Appreciate the help

              this thing looks like it was written by an epileptic ferret Dave Kreskowiak

              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