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

    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
    • 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
      #22

      The original code was mangled because of fixes required for smileys. Here is the correct code:

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

      P 1 Reply Last reply
      0
      • B bscaer

        The original code was mangled because of fixes required for smileys. Here is the correct code:

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

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

        What I posted works fine for me (once the SPACEs are removed).

        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