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. Add spaces to large numbers

Add spaces to large numbers

Scheduled Pinned Locked Moved C#
question
5 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.
  • D Offline
    D Offline
    Deques
    wrote on last edited by
    #1

    Is there a way to do it? IE: 30000 I want it to be 30 000

    O 1 Reply Last reply
    0
    • D Deques

      Is there a way to do it? IE: 30000 I want it to be 30 000

      O Offline
      O Offline
      OsoreWatashi
      wrote on last edited by
      #2

      You can use the .ToString() method to make it to a string. There is a regexp function to do such a thing, but I forgot about it. However, here is snippet that should do the trick aswell.

      int number = 3000000;
      int lengthPerSplit = 3;
      string splitText = " ";

      string result = number.ToString();
      for (int index = result.Length - lengthPerSplit; index > 0; index -= lengthPerSplit)
      result = result.Substring(0, index) + splitText + result.Substring(index, result.Length - index);

      D M 2 Replies Last reply
      0
      • O OsoreWatashi

        You can use the .ToString() method to make it to a string. There is a regexp function to do such a thing, but I forgot about it. However, here is snippet that should do the trick aswell.

        int number = 3000000;
        int lengthPerSplit = 3;
        string splitText = " ";

        string result = number.ToString();
        for (int index = result.Length - lengthPerSplit; index > 0; index -= lengthPerSplit)
        result = result.Substring(0, index) + splitText + result.Substring(index, result.Length - index);

        D Offline
        D Offline
        Deques
        wrote on last edited by
        #3

        Thanks, it works

        M 1 Reply Last reply
        0
        • O OsoreWatashi

          You can use the .ToString() method to make it to a string. There is a regexp function to do such a thing, but I forgot about it. However, here is snippet that should do the trick aswell.

          int number = 3000000;
          int lengthPerSplit = 3;
          string splitText = " ";

          string result = number.ToString();
          for (int index = result.Length - lengthPerSplit; index > 0; index -= lengthPerSplit)
          result = result.Substring(0, index) + splitText + result.Substring(index, result.Length - index);

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

          string.Format is probably the easiest way to get any kind of formatting you want.

          There are three kinds of people in the world - those who can count and those who can't...

          1 Reply Last reply
          0
          • D Deques

            Thanks, it works

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            try this as a simpler solution, just keep adding sets of 3 #s string.Format("{0:### ### ###.00}", 30000000);

            Never underestimate the power of human stupidity RAH

            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