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 to display only numbers after comma in c#

How to display only numbers after comma in c#

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
12 Posts 5 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.
  • M Offline
    M Offline
    mm310
    wrote on last edited by
    #1

    i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?

    S P L G 4 Replies Last reply
    0
    • M mm310

      i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?

      S Offline
      S Offline
      Sean89
      wrote on last edited by
      #2

      Heres a simple way:

      static void Main(string[] args)
      {
          double x = 33.452;
          string number = x.ToString();
          char[] sperator = { '.' };
          string[] seperate = number.Split(sperator);
          double xSplit = Convert.ToDouble(seperate[1]);
          Console.WriteLine(xSplit);
      }
      

      That will output 452 to the console. There might be an easier way though I'm not sure. Hope that helps ;P

      P 1 Reply Last reply
      0
      • M mm310

        i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?

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

        Probably and eaiser way...But this works. double d = 3.14; string s = d.ToString(); char[] delimiter = { '.' }; string[] a = s.Split(delimiter); Console.WriteLine(a[1]); gl-Paul

        S 1 Reply Last reply
        0
        • P prrusa

          Probably and eaiser way...But this works. double d = 3.14; string s = d.ToString(); char[] delimiter = { '.' }; string[] a = s.Split(delimiter); Console.WriteLine(a[1]); gl-Paul

          S Offline
          S Offline
          Sean89
          wrote on last edited by
          #4

          I put the exact same thing 8 min eariler :confused:

          P 1 Reply Last reply
          0
          • S Sean89

            Heres a simple way:

            static void Main(string[] args)
            {
                double x = 33.452;
                string number = x.ToString();
                char[] sperator = { '.' };
                string[] seperate = number.Split(sperator);
                double xSplit = Convert.ToDouble(seperate[1]);
                Console.WriteLine(xSplit);
            }
            

            That will output 452 to the console. There might be an easier way though I'm not sure. Hope that helps ;P

            P Offline
            P Offline
            prrusa
            wrote on last edited by
            #5

            I like your's with the colored background better...:)

            1 Reply Last reply
            0
            • S Sean89

              I put the exact same thing 8 min eariler :confused:

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

              didn't see your post...From your '???' do you not realize how two people can post within an 8 minute perion?

              S 1 Reply Last reply
              0
              • P prrusa

                didn't see your post...From your '???' do you not realize how two people can post within an 8 minute perion?

                S Offline
                S Offline
                Sean89
                wrote on last edited by
                #7

                I'm not insulting you I just thought it was kind of funny that we said the exact same thing.

                P 1 Reply Last reply
                0
                • S Sean89

                  I'm not insulting you I just thought it was kind of funny that we said the exact same thing.

                  P Offline
                  P Offline
                  prrusa
                  wrote on last edited by
                  #8

                  Hopefully its' a good thing....

                  S 1 Reply Last reply
                  0
                  • P prrusa

                    Hopefully its' a good thing....

                    S Offline
                    S Offline
                    Sean89
                    wrote on last edited by
                    #9

                    These message boards are practically like instant messaging lol.

                    P 1 Reply Last reply
                    0
                    • S Sean89

                      These message boards are practically like instant messaging lol.

                      P Offline
                      P Offline
                      prrusa
                      wrote on last edited by
                      #10

                      yes...Coding today is almost too easy with all the help availible in realtime...;)

                      1 Reply Last reply
                      0
                      • M mm310

                        i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #11

                        Unfortunately the other solutions will fail under other cultures. Do the following instead:

                        double x = 33.452;
                        Console.WriteLine(x % Math.Floor(x));

                        xacc.ide-0.1.3.2

                        1 Reply Last reply
                        0
                        • M mm310

                          i need print only numbers after comma example double x=33.452 Console.WriteLine(x); output 452 how I do this ?

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

                          This works for any culture. It also handles the case where there is no fractional digits, instead of causing an exception.

                          double x = 33.452;
                          string number = x.ToString(CultureInfo.InvariantCulture);
                          int pos = number.IndexOf('.');
                          if (pos == -1) {
                          Controle.WriteLine();
                          } else {
                          Console.WriteLine(number.Substring(pos + 1));
                          }

                          --- b { font-weight: normal; }

                          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