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. Numeric formatting question

Numeric formatting question

Scheduled Pinned Locked Moved C#
question
23 Posts 6 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.
  • B bscaer

    In the following String.Format call it will format a number to 6 zero-padded digits, prepend the plus or minus sign and then pad with a space on the left to 8 characters: String.Format({0,8:+000000;-000000}, num); // result is of the form " +002009" What format string can I use to format a number to 7 space-padded digits and prepend the plus or minus sign before the spaces? I am looking for something like "+ 2009" Beth

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #2

    bscaer wrote:

    What format string can I use

    I don't know but they have probably hidden that information in the documentation[^] We keep complaining about that but they just keep doing it. Oh well.

    B P 2 Replies Last reply
    0
    • L led mike

      bscaer wrote:

      What format string can I use

      I don't know but they have probably hidden that information in the documentation[^] We keep complaining about that but they just keep doing it. Oh well.

      B Offline
      B Offline
      bscaer
      wrote on last edited by
      #3

      Thanks for the link. I have spent quite a bit of time reading the documentation but it seems to be incomplete, at best.

      L 1 Reply Last reply
      0
      • B bscaer

        In the following String.Format call it will format a number to 6 zero-padded digits, prepend the plus or minus sign and then pad with a space on the left to 8 characters: String.Format({0,8:+000000;-000000}, num); // result is of the form " +002009" What format string can I use to format a number to 7 space-padded digits and prepend the plus or minus sign before the spaces? I am looking for something like "+ 2009" Beth

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

        Will this suit the need? :-D

        System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , 2009 ) ;
        System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , -2009 ) ;

        (SPACEs added to avoid smileys; remove them.) (Third try.)

        modified on Friday, February 27, 2009 1:55 PM

        B D L 4 Replies Last reply
        0
        • P PIEBALDconsult

          Will this suit the need? :-D

          System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , 2009 ) ;
          System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , -2009 ) ;

          (SPACEs added to avoid smileys; remove them.) (Third try.)

          modified on Friday, February 27, 2009 1:55 PM

          B Offline
          B Offline
          bscaer
          wrote on last edited by
          #5

          Yes, that is brilliant! Thank you :cool:

          P 1 Reply Last reply
          0
          • B bscaer

            Thanks for the link. I have spent quite a bit of time reading the documentation but it seems to be incomplete, at best.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #6

            bscaer wrote:

            Thanks for the link.

            That was a bad link, sorry. Standard number formats[^] Custome format strings[^]

            bscaer wrote:

            but it seems to be incomplete

            Not the documentation. Sometimes all the information you need is not on a single page but there are links to the additional pages. Perhaps the formatting support does not provide for your specific goal. If that is the case then you will have to provide your own implementation.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Will this suit the need? :-D

              System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , 2009 ) ;
              System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , -2009 ) ;

              (SPACEs added to avoid smileys; remove them.) (Third try.)

              modified on Friday, February 27, 2009 1:55 PM

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #7

              similes avoided spaces. :doh:

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

              P 1 Reply Last reply
              0
              • D Dan Neely

                similes avoided spaces. :doh:

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

                Alas. :sigh:

                1 Reply Last reply
                0
                • L led mike

                  bscaer wrote:

                  What format string can I use

                  I don't know but they have probably hidden that information in the documentation[^] We keep complaining about that but they just keep doing it. Oh well.

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

                  I don't think she would have gotten as far as she did without having read the documentation; I know I wouldn't have.

                  1 Reply Last reply
                  0
                  • B bscaer

                    In the following String.Format call it will format a number to 6 zero-padded digits, prepend the plus or minus sign and then pad with a space on the left to 8 characters: String.Format({0,8:+000000;-000000}, num); // result is of the form " +002009" What format string can I use to format a number to 7 space-padded digits and prepend the plus or minus sign before the spaces? I am looking for something like "+ 2009" Beth

                    E Offline
                    E Offline
                    Ennis Ray Lynch Jr
                    wrote on last edited by
                    #10

                    The best thing to do in this scenario is to create your own customer formatter and then pass it in as an argument. String.Format(formatProvider, formatString, arguments). I have found this to be the only good way when dealing with phone numbers which may have bad data and I am sure it will provide you with that you need. http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx[^]

                    Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                    P 1 Reply Last reply
                    0
                    • B bscaer

                      Yes, that is brilliant! Thank you :cool:

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

                      Glad to be of service. I was also just looking for a solution using my ApplyFormat[^] method, but no joy. I had hoped that this would do it:

                      System.Console.WriteLine ( 2009.ApplyFormat ( "' '+;-''#####0;#####0" ) ) ;
                      System.Console.WriteLine ( (-2009).ApplyFormat ( "' '+;-''#####0;#####0" ) ) ;

                      Apparently the number sign (#) format character doesn't perform padding. :mad: (Apparently its only purpose is to result in an empty string when the value is zero. Yeah, like that's useful. :rolleyes: ) I guess that means I can add more functionality to my method to support it. Yippee, something to do.

                      B 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Will this suit the need? :-D

                        System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , 2009 ) ;
                        System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , -2009 ) ;

                        (SPACEs added to avoid smileys; remove them.) (Third try.)

                        modified on Friday, February 27, 2009 1:55 PM

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

                        PIEBALDconsult wrote:

                        SPACEs added to avoid smileys; remove them.

                        Whenever 4 or more spaces are present use t:)a:)b:)s

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                        modified on Sunday, June 12, 2011 8:46 AM

                        P 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          PIEBALDconsult wrote:

                          SPACEs added to avoid smileys; remove them.

                          Whenever 4 or more spaces are present use t:)a:)b:)s

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                          modified on Sunday, June 12, 2011 8:46 AM

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

                          I didn't want any SPACEs in the format at all.

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Glad to be of service. I was also just looking for a solution using my ApplyFormat[^] method, but no joy. I had hoped that this would do it:

                            System.Console.WriteLine ( 2009.ApplyFormat ( "' '+;-''#####0;#####0" ) ) ;
                            System.Console.WriteLine ( (-2009).ApplyFormat ( "' '+;-''#####0;#####0" ) ) ;

                            Apparently the number sign (#) format character doesn't perform padding. :mad: (Apparently its only purpose is to result in an empty string when the value is zero. Yeah, like that's useful. :rolleyes: ) I guess that means I can add more functionality to my method to support it. Yippee, something to do.

                            B Offline
                            B Offline
                            bscaer
                            wrote on last edited by
                            #14

                            From my experimentation it seems like the number sign (#) is only useful when specifying the location of a special character, like a comma, such as in String.Format("0:###,###,###").

                            P D 2 Replies Last reply
                            0
                            • B bscaer

                              From my experimentation it seems like the number sign (#) is only useful when specifying the location of a special character, like a comma, such as in String.Format("0:###,###,###").

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

                              Ah, that too. Darn, there needs to be another option, like '9' to specify left-padding. I've forgotten everything I learned about COBOL, so I forget how this was handled in it.

                              L 1 Reply Last reply
                              0
                              • E Ennis Ray Lynch Jr

                                The best thing to do in this scenario is to create your own customer formatter and then pass it in as an argument. String.Format(formatProvider, formatString, arguments). I have found this to be the only good way when dealing with phone numbers which may have bad data and I am sure it will provide you with that you need. http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx[^]

                                Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

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

                                It just might come to that. :sigh:

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Ah, that too. Darn, there needs to be another option, like '9' to specify left-padding. I've forgotten everything I learned about COBOL, so I forget how this was handled in it.

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

                                  PIEBALDconsult wrote:

                                  I've forgotten everything I learned about COBOL, so I forget how this was handled in it.

                                  You can fix that easily by following some links at the bottom[^]. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                  P 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    PIEBALDconsult wrote:

                                    I've forgotten everything I learned about COBOL, so I forget how this was handled in it.

                                    You can fix that easily by following some links at the bottom[^]. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles]


                                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


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

                                    I already did some searching; I now know almost as much about COBOL as I used to. :-D

                                    1 Reply Last reply
                                    0
                                    • B bscaer

                                      From my experimentation it seems like the number sign (#) is only useful when specifying the location of a special character, like a comma, such as in String.Format("0:###,###,###").

                                      D Offline
                                      D Offline
                                      Dan Neely
                                      wrote on last edited by
                                      #19

                                      IIRC it also lets you pad to the right of a decimal. Unfortunately I don't know how to pad to the left.

                                      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                      P 1 Reply Last reply
                                      0
                                      • D Dan Neely

                                        IIRC it also lets you pad to the right of a decimal. Unfortunately I don't know how to pad to the left.

                                        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

                                        I appears that the formats themselves don't pad (other than with zeroes), that padding is handled by the WriteLine, string.Format, etc. that calls it. I assume that the creators of the formatters decided that if the caller was going to do the padding, then they didn't need to. Which makes sense, but there are cases when the caller isn't going to do the padding so the formatter should be able to. Because my ApplyFormat method is a caller of formatting, I now have to decide how best to allow the user to specify padding. I'm thinking I should follow Microsoft's lead and allow the format to be prefixed with "n:" to specify a minimum width of n characters.

                                        1 Reply Last reply
                                        0
                                        • B bscaer

                                          In the following String.Format call it will format a number to 6 zero-padded digits, prepend the plus or minus sign and then pad with a space on the left to 8 characters: String.Format({0,8:+000000;-000000}, num); // result is of the form " +002009" What format string can I use to format a number to 7 space-padded digits and prepend the plus or minus sign before the spaces? I am looking for something like "+ 2009" Beth

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

                                          I have added padding support to my ApplyFormat method, so now 2009.ApplyFormat ( "' '+;-''6 : 0 ;0" ) will do that. But now I want to cache the interpretation of the format specifier to streamline things a bit, so I'm not yet ready to update the article.

                                          modified on Saturday, February 28, 2009 6:26 PM

                                          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