string/output formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john coltrane

    string/output formatting

    Is there way to create a formatted string in a similar that is similar to
    sprintf?
    The same for printing, printf?

    C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
    and hex but these do not seem to allow for specifying the number of
    decimals, left/right placement, or string formatting.

    Thanks

    john



  • Jon Skeet [C# MVP]

    #2
    Re: string/output formatting

    john coltrane <tendengarci@ya hoo.comwrote:
    Is there way to create a formatted string in a similar that is similar to
    sprintf?
    The same for printing, printf?
    >
    C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
    and hex but these do not seem to allow for specifying the number of
    decimals, left/right placement, or string formatting.
    Yes, they do.

    Look up "standard numeric format strings" and "custom numeric format
    strings" in MSDN.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

      #3
      RE: string/output formatting

      Number formats will allow codes like these.
      C is for currency and will output based on currency formatting options for
      machine the code is running on
      F is for a fixed point number and allows you to specify the number of
      decimal places. e.g for 2 decimal places, write F2
      There are others which are documented in the MSDN but here is a good guide
      you can print:



      --
      Ciaran O''Donnell



      "john coltrane" wrote:
      Is there way to create a formatted string in a similar that is similar to
      sprintf?
      The same for printing, printf?
      >
      C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,
      and hex but these do not seem to allow for specifying the number of
      decimals, left/right placement, or string formatting.
      >
      Thanks
      >
      john
      >
      >
      >
      >

      Comment

      • john coltrane

        #4
        Re: string/output formatting

        Ciaran,
        thanks, with printf I can do f10.2 to specify the field length and the
        number of decimals. It seems that this functionality does not exist in the
        FCL. Also, there does not seem to be a way to specify the field size for a
        string. I guess I will have to construct this myself.

        john

        "Ciaran O''Donnell" <CiaranODonnell @discussions.mi crosoft.comwrot e in
        message news:2A86E3DF-B168-4BC4-B06D-E185DA260FAD@mi crosoft.com...
        Number formats will allow codes like these.
        C is for currency and will output based on currency formatting options for
        machine the code is running on
        F is for a fixed point number and allows you to specify the number of
        decimal places. e.g for 2 decimal places, write F2
        There are others which are documented in the MSDN but here is a good guide
        you can print:
        >

        >
        --
        Ciaran O''Donnell

        >
        >
        "john coltrane" wrote:
        >
        >Is there way to create a formatted string in a similar that is similar to
        >sprintf?
        >The same for printing, printf?
        >>
        >C,D,E,F,G,N, X for currency, decimal, exponential, fixed, general,
        >numerical,
        >and hex but these do not seem to allow for specifying the number of
        >decimals, left/right placement, or string formatting.
        >>
        >Thanks
        >>
        >john
        >>
        >>
        >>
        >>
        >

        Comment

        • john coltrane

          #5
          Re: string/output formatting

          I would like to be able to specify the size and number of decimals of a
          number and whether the field is left or right justified. Also, there doesn't
          seem to be a way to specify the size of a string field. In other words I
          would like the functionality of printf formatting.

          Numeric formatting, n:10, results in "100.0000000000 " instead of "
          100".



          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:MPG.229d1f 9cd4964e27ccf@m snews.microsoft .com...
          john coltrane <tendengarci@ya hoo.comwrote:
          >Is there way to create a formatted string in a similar that is similar to
          >sprintf?
          >The same for printing, printf?
          >>
          >C,D,E,F,G,N, X for currency, decimal, exponential, fixed, general,
          >numerical,
          >and hex but these do not seem to allow for specifying the number of
          >decimals, left/right placement, or string formatting.
          >
          Yes, they do.
          >
          Look up "standard numeric format strings" and "custom numeric format
          strings" in MSDN.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com
          >

          Comment

          • Abubakar

            #6
            Re: string/output formatting

            Or maybe just create a little c dll that would call printf for you, and you
            can call that from c#.

            ...ab

            "john coltrane" <tendengarci@ya hoo.comwrote in message
            news:OaDoxcquIH A.3564@TK2MSFTN GP03.phx.gbl...
            string. I guess I will have to construct this myself.
            >

            Comment

            • Jeroen Mostert

              #7
              Re: string/output formatting

              Abubakar wrote:
              Or maybe just create a little c dll that would call printf for you, and
              you can call that from c#.
              >
              Don't do this -- performance hit, unnecessarily requiring P/Invoke
              permissions, buffer overflow concerns, additional build targets and last and
              in this case least an extra binary to deploy. In short, this is inferior to
              learning how formatting works in C# (and how you can extend the mechanism
              yourself) in just about every way except possibly development time, but I'd
              consider it an investment.

              --
              J.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: string/output formatting

                john coltrane <tendengarci@ya hoo.comwrote:
                I would like to be able to specify the size and number of decimals of a
                number and whether the field is left or right justified. Also, there doesn't
                seem to be a way to specify the size of a string field. In other words I
                would like the functionality of printf formatting.
                >
                Numeric formatting, n:10, results in "100.0000000000 " instead of "
                100".
                You shouldn't give up just because your first attempt didn't do what
                you wanted to (although it *did* do what the documentation stated).
                But:

                If you want " 100":
                Console.WriteLi ne("{0,10:N0}" , 100);

                Left aligned string, expanded to 5 chars:
                Console.WriteLi ne("{0,-5}", "abc");

                Right aligned string, expanded to 5 chars:
                Console.WriteLi ne("{0,5}", "abc");

                I can't immediately see a way of making it trim as well as expanding,
                admittedly - but it's not clear whether or not you wanted that.

                --
                Jon Skeet - <skeet@pobox.co m>
                Web site: http://www.pobox.com/~skeet
                Blog: http://www.msmvps.com/jon.skeet
                C# in Depth: http://csharpindepth.com

                Comment

                • John Coltrane

                  #9
                  Re: string/output formatting

                  Jeroen Mostert wrote:
                  Abubakar wrote:
                  >Or maybe just create a little c dll that would call printf for you,
                  >and you can call that from c#.
                  >>
                  Don't do this -- performance hit, unnecessarily requiring P/Invoke
                  permissions, buffer overflow concerns, additional build targets and last
                  and in this case least an extra binary to deploy. In short, this is
                  inferior to learning how formatting works in C# (and how you can extend
                  the mechanism yourself) in just about every way except possibly
                  development time, but I'd consider it an investment.
                  >
                  Thanks Jeroen,
                  I was hoping that C# or the .Net Framework would provide more robust
                  formatting functionality. Extending IFormatProvider and/or
                  ICustomFormatte r would involve a lot of work to get to provide the same
                  functionality as printf. The limited functionality using the format
                  types C, D, E, F, G, N, P, R, and X just doesn't meet my needs.

                  oh well

                  Comment

                  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                    #10
                    Re: string/output formatting

                    John Coltrane wrote:
                    I was hoping that C# or the .Net Framework would provide more robust
                    formatting functionality. Extending IFormatProvider and/or
                    ICustomFormatte r would involve a lot of work to get to provide the same
                    functionality as printf. The limited functionality using the format
                    types C, D, E, F, G, N, P, R, and X just doesn't meet my needs.
                    ..NET has excellent formatting capabilities.

                    But less excellent documentation of them.

                    Try read:


                    Arne

                    Comment

                    Working...