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. can i "%.1f" in C#?

can i "%.1f" in C#?

Scheduled Pinned Locked Moved C#
questioncsharp
4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    In my aged c code, there is tons of "%.1f" in source. How can i transfer it to c#? i read the document of Console.Write. the format is describe by {a,b:s}. How can i get the same result as c format string like "%.1f"?

    J T M 3 Replies Last reply
    0
    • L Lost User

      In my aged c code, there is tons of "%.1f" in source. How can i transfer it to c#? i read the document of Console.Write. the format is describe by {a,b:s}. How can i get the same result as c format string like "%.1f"?

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Are you asking how you can use "%.1f" in C# or for the format string that produces an equivilant to "%.1f"? James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

      1 Reply Last reply
      0
      • L Lost User

        In my aged c code, there is tons of "%.1f" in source. How can i transfer it to c#? i read the document of Console.Write. the format is describe by {a,b:s}. How can i get the same result as c format string like "%.1f"?

        T Offline
        T Offline
        Tom Archer
        wrote on last edited by
        #3

        You can use the Math.Round static function or the String.Format method (called implicitly below). double d = 136.7835; Console.WriteLine(Math.Round(d, 1)); Console.WriteLine("{0:.#}", d); Both of these result in 136.8 which is what the C/C++ format specifier %.1f would provide. Cheers, Tom Archer Author, Inside C#

        1 Reply Last reply
        0
        • L Lost User

          In my aged c code, there is tons of "%.1f" in source. How can i transfer it to c#? i read the document of Console.Write. the format is describe by {a,b:s}. How can i get the same result as c format string like "%.1f"?

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

          Console.WriteLine("Test: {0:f1}", 765.7689); The result will be 765.8 instead of 765.7, but usually that is the preferred output... /moliate (just started learning C#)

          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