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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C#'s sprintf

C#'s sprintf

Scheduled Pinned Locked Moved C#
questioncsharp
6 Posts 2 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.
  • A Offline
    A Offline
    ABean
    wrote on last edited by
    #1

    Is there an equivalent to sprintf in C#? Basically I have some C# "value types" (floats, decimals) that I would like to format into a string variable. Also, I have a DateTime variable that I would like to format into a string variable. What is the best way to do this? Thanks.

    H A 2 Replies Last reply
    0
    • A ABean

      Is there an equivalent to sprintf in C#? Basically I have some C# "value types" (floats, decimals) that I would like to format into a string variable. Also, I have a DateTime variable that I would like to format into a string variable. What is the best way to do this? Thanks.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      See String.Format in the .NET Framework SDK documentation. Many types - like the primitive types - implement IFormatProvider and have an overloaded ToString method which accepts the same format specifiers as you'd pass in String.Format:

      double d = 1.5;
      string a = string.Format("{0:C}", d);
      string b = d.ToString("C");
      Console.WriteLine(a == b); // Prints "True"

      Other methods like StringBuilder.AppendFormat and Console.WriteLine can also accept format specifiers with parameter indexes. For more information, also see my article, Custom String Formatting in .NET[^]. I also mention several reasons why String.Format and all the classes, methods, and interfaces that comprise formatting in .NET are better than any printf-like function.

      Microsoft MVP, Visual C# My Articles

      A 1 Reply Last reply
      0
      • A ABean

        Is there an equivalent to sprintf in C#? Basically I have some C# "value types" (floats, decimals) that I would like to format into a string variable. Also, I have a DateTime variable that I would like to format into a string variable. What is the best way to do this? Thanks.

        A Offline
        A Offline
        ABean
        wrote on last edited by
        #3

        I think I got it. . . Its done by passing args to the ToString() method.

        H 1 Reply Last reply
        0
        • H Heath Stewart

          See String.Format in the .NET Framework SDK documentation. Many types - like the primitive types - implement IFormatProvider and have an overloaded ToString method which accepts the same format specifiers as you'd pass in String.Format:

          double d = 1.5;
          string a = string.Format("{0:C}", d);
          string b = d.ToString("C");
          Console.WriteLine(a == b); // Prints "True"

          Other methods like StringBuilder.AppendFormat and Console.WriteLine can also accept format specifiers with parameter indexes. For more information, also see my article, Custom String Formatting in .NET[^]. I also mention several reasons why String.Format and all the classes, methods, and interfaces that comprise formatting in .NET are better than any printf-like function.

          Microsoft MVP, Visual C# My Articles

          A Offline
          A Offline
          ABean
          wrote on last edited by
          #4

          Thanks Heath, You beat me to my post below. :)

          1 Reply Last reply
          0
          • A ABean

            I think I got it. . . Its done by passing args to the ToString() method.

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            You still might want to look at what I wrote. Calling the overloaded ToString to format types isn't always the best way. If you look at my article about custom format providers, you might notice why. When using String.Format (or similar methods), you can localize your application much easier because you can localize the entire format string and not have to worry about parameter order since you specify the parameter indexes in the format string. If all you're doing is printing a Type (like some primitive), then using ToString is fine. You should also keep in mind that the overridden ToString uses a well-defined output (typically the same as the "G" format specifier, although this is only a guidelines). I also have several links in my article to more information in .NET Framework SDK. No, I'm not trying to market my article, I just don't feel like copying and pasting all the links. :) There's just a lot more power in string formatting within .NET and it's worth understanding.

            Microsoft MVP, Visual C# My Articles

            A 1 Reply Last reply
            0
            • H Heath Stewart

              You still might want to look at what I wrote. Calling the overloaded ToString to format types isn't always the best way. If you look at my article about custom format providers, you might notice why. When using String.Format (or similar methods), you can localize your application much easier because you can localize the entire format string and not have to worry about parameter order since you specify the parameter indexes in the format string. If all you're doing is printing a Type (like some primitive), then using ToString is fine. You should also keep in mind that the overridden ToString uses a well-defined output (typically the same as the "G" format specifier, although this is only a guidelines). I also have several links in my article to more information in .NET Framework SDK. No, I'm not trying to market my article, I just don't feel like copying and pasting all the links. :) There's just a lot more power in string formatting within .NET and it's worth understanding.

              Microsoft MVP, Visual C# My Articles

              A Offline
              A Offline
              ABean
              wrote on last edited by
              #6

              Heath, I just read your article; thanks for posting it. It brings together all the MSDN articles that I had read and the ones that I missed and it cuts straight to the point. Thanks again.

              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