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. How do you count the number of decimals in a decimal datatype

How do you count the number of decimals in a decimal datatype

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

    I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.

    L T M 3 Replies Last reply
    0
    • N Nathan Revka

      I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.

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

      Yes. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      1 Reply Last reply
      0
      • N Nathan Revka

        I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.

        T Offline
        T Offline
        TheGreatAndPowerfulOz
        wrote on last edited by
        #3

        subtract the integer part multiply by 100 subtract the integer part, again if result not equal to 0 then you've got a problem.

        1 Reply Last reply
        0
        • N Nathan Revka

          I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          well if you are using a textbox then you will need a number of different validations... 1. Check the currency symbol exists only once and is the first character, if not present you should auto add it 2. Check there is only 1 decimal point 3. Check for any , symbols you may want to include and insure they are in the right places 4. Check only two digits after the decimal place... if(text.LastIndexOf('.') != text.Length - 3) //not two digits after the decimal point ...there are other validations you may want to try and other methods to calculate the number of digits after a decimal. you could also use a NumericUpDown control which has a property to set how many decimal places you wish to use, in addition to choosing if you want to you commas for thousand seperators

          Life goes very fast. Tomorrow, today is already yesterday.

          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