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. Specifying number of digits after decimal in a fixed-point formatted string

Specifying number of digits after decimal in a fixed-point formatted string

Scheduled Pinned Locked Moved C#
csharpc++helpquestion
4 Posts 3 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.
  • D Offline
    D Offline
    DTWC_Lawrence
    wrote on last edited by
    #1

    Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B

    A S 2 Replies Last reply
    0
    • D DTWC_Lawrence

      Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B

      A Offline
      A Offline
      Alex A Miller
      wrote on last edited by
      #2

      double newW = 12.345678; string pw = string.Format("{0:N3}", newW); //pw is now 12.345

      D 1 Reply Last reply
      0
      • A Alex A Miller

        double newW = 12.345678; string pw = string.Format("{0:N3}", newW); //pw is now 12.345

        D Offline
        D Offline
        DTWC_Lawrence
        wrote on last edited by
        #3

        As always it is very humbling to have the forest pointed out! Thanks for the quick reply. B

        1 Reply Last reply
        0
        • D DTWC_Lawrence

          Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B

          S Offline
          S Offline
          Salil Khedkar
          wrote on last edited by
          #4

          Try this double MyFloat = 123.456; string MyString; MyString = String.Format("{0:00.00}", MyFloat); Console.WriteLine(MyString); or even better ;) double MyFloat = 123.456; Console.WriteLine(String.Format("{0:00.00}", MyFloat)); Salil Khedkar [^]

          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