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. how to set digits precision of a float

how to set digits precision of a float

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 5 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.
  • U Offline
    U Offline
    uowzd01
    wrote on last edited by
    #1

    Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.

    C B S 3 Replies Last reply
    0
    • U uowzd01

      Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      for rendering, you can use string.format("0.###"), with a # for each level of precision. You can't change what a float can store, however.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      U 1 Reply Last reply
      0
      • C Christian Graus

        for rendering, you can use string.format("0.###"), with a # for each level of precision. You can't change what a float can store, however.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        U Offline
        U Offline
        uowzd01
        wrote on last edited by
        #3

        Thank you very much for your quck help, you gave me a thought of doing it in a different way, the actual code is String.format({0:#.##}, float).

        C S 2 Replies Last reply
        0
        • U uowzd01

          Thank you very much for your quck help, you gave me a thought of doing it in a different way, the actual code is String.format({0:#.##}, float).

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Yes, that also works.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          • U uowzd01

            Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.

            B Offline
            B Offline
            Bala subramanyam M
            wrote on last edited by
            #5

            Console.WriteLine("{0:N2}",value);

            (M.BALA SUBRAMANYAM)

            S 1 Reply Last reply
            0
            • U uowzd01

              Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.

              S Offline
              S Offline
              Sujith C Jose
              wrote on last edited by
              #6

              :) Or simply use Math.Round(floatvalue,2);

              Sujith

              S 1 Reply Last reply
              0
              • U uowzd01

                Thank you very much for your quck help, you gave me a thought of doing it in a different way, the actual code is String.format({0:#.##}, float).

                S Offline
                S Offline
                Saeed Jafarian
                wrote on last edited by
                #7

                hi i have a code like this: float f = d / (c * b * a) / 1000000; string.Format("{0:0.00}",f); txtdansite1.Text = f.ToString(); it still does not work and it has the float`s default precision. help me please

                1 Reply Last reply
                0
                • B Bala subramanyam M

                  Console.WriteLine("{0:N2}",value);

                  (M.BALA SUBRAMANYAM)

                  S Offline
                  S Offline
                  Saeed Jafarian
                  wrote on last edited by
                  #8

                  in win application we don`t use console and this syntax does not help,for example: textbox1.text=string.format("{0:n2}", variable); and the textbox1 shows: 0.00

                  1 Reply Last reply
                  0
                  • S Sujith C Jose

                    :) Or simply use Math.Round(floatvalue,2);

                    Sujith

                    S Offline
                    S Offline
                    Saeed Jafarian
                    wrote on last edited by
                    #9

                    float f=2.37037E-08 Math.Round(f, 2); textbox1.Text = f.ToString(); still the same problem and the textbox shows 2.37037E-08 :( :((

                    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