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. Count Decimals

Count Decimals

Scheduled Pinned Locked Moved C#
algorithmsregexquestiondiscussion
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.
  • K Offline
    K Offline
    keyboard warrior
    wrote on last edited by
    #1

    Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?

    ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

    S P G 3 Replies Last reply
    0
    • K keyboard warrior

      Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?

      ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

      S Offline
      S Offline
      Sean Michael Murphy
      wrote on last edited by
      #2

      jgasm wrote:

      I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple.

      If you know the string always has a decimal:

      Int32 decimalCount = text.Split('.')[1].Length

      If it doesn't always have a decimal, it gets much more complicated :):

      Int32 decimalCount = text.IndexOf('.') > -1 ? text.Split('.')[1].Length : 0;

      Share and enjoy. Sean

      1 Reply Last reply
      0
      • K keyboard warrior

        Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?

        ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Count significant decimal places? This is rather rough:

        [System.STAThreadAttribute]
        static void Main(string[] args)
        {
        int d ;

        args \[ 0 \] = args \[ 0 \].Trim( new char\[\] { '0' } ) ;
        d = args \[ 0 \].IndexOf ( '.' ) ;
        
        if ( d >= 0 )
        {
            d = args \[ 0 \].Length - d - 1 ;
        }
        else
        {
            d = 0 ;
        }
        
        System.Console.WriteLine ( d ) ;
        

        }

        K 1 Reply Last reply
        0
        • P PIEBALDconsult

          Count significant decimal places? This is rather rough:

          [System.STAThreadAttribute]
          static void Main(string[] args)
          {
          int d ;

          args \[ 0 \] = args \[ 0 \].Trim( new char\[\] { '0' } ) ;
          d = args \[ 0 \].IndexOf ( '.' ) ;
          
          if ( d >= 0 )
          {
              d = args \[ 0 \].Length - d - 1 ;
          }
          else
          {
              d = 0 ;
          }
          
          System.Console.WriteLine ( d ) ;
          

          }

          K Offline
          K Offline
          keyboard warrior
          wrote on last edited by
          #4

          Thank you so much for the reply. :) However, Sean's answer was exactly what I was looking for.

          ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

          P 1 Reply Last reply
          0
          • K keyboard warrior

            Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?

            ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            A similar question was asked in the microsoft.public.dotnet.languages.vb newsgroup two days ago. Here's the code that I suggested, converted to C#, and assuming that the decimal separator is a period:

            int pos = text.IndexOf('.');
            int decimalCount;
            if (pos == -1) {
            decimalCount = 0;
            } else {
            decimalCount = value.Length - pos - 1;
            }

            It's about ten times faster than splitting the string. :) If you want it in few lines:

            int pos = text.IndexOf('.');
            int decimalCount = (pos == -1) ? 0 : value.Length - pos - 1;

            --- single minded; short sighted; long gone;

            1 Reply Last reply
            0
            • K keyboard warrior

              Thank you so much for the reply. :) However, Sean's answer was exactly what I was looking for.

              ----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              If you don't need the results of the Split for something else then mine is likely more efficient. Here it is with the Conditional Operator:

              int d ;

              args [ 0 ] = args [ 0 ].Trim( new char[] { '0' } ) ;

              System.Console.WriteLine
              (
              ( d = args [ 0 ].IndexOf ( '.' ) ) == -1 ? 0 : args [ 0 ].Length - d - 1
              ) ;

              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