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 / C++ / MFC
  4. Rounding numbers (both ways)

Rounding numbers (both ways)

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiotutorialquestion
9 Posts 6 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.
  • J Offline
    J Offline
    Johpoke
    wrote on last edited by
    #1

    Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

    /Johannes

    J T D D realJSOPR 5 Replies Last reply
    0
    • J Johpoke

      Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

      /Johannes

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      Johpoke wrote:

      For example mathematically this; 0.306-> 0.31 0.99-> 1.00

      This is int integer = (int)(0.306 + 0.5);

      Johpoke wrote:

      and string wise; 0.12000000 -> 0.12 12.0000000 -> 12

      Maybe boosts format library[^] can help you?


      "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

      1 Reply Last reply
      0
      • J Johpoke

        Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

        /Johannes

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        Johpoke wrote:

        0.306-> 0.31 0.99-> 1.00

        this is a matter of representation, so basically, printf() does it ! for the 2 questions btw, you can have a look here[^]


        [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

        1 Reply Last reply
        0
        • J Johpoke

          Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

          /Johannes

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          There's also the ceil() and floor() functions. It's really just a matter of trying each suggestion until you find the one that does just what you need.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • J Johpoke

            Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

            /Johannes

            D Offline
            D Offline
            Dy
            wrote on last edited by
            #5

            Strings: CString str; float f = 0.306; //float f = 12.0000000f; str.Format(_T("%.2g"), f); TRACE(_T("%s\n"), str); Because I've used %g (instead of %f), 12.0 will be 12. Numerics: Take a look at this: http://www.codeproject.com/cpp/floatutils.asp[^]


            - Dy

            J 1 Reply Last reply
            0
            • D Dy

              Strings: CString str; float f = 0.306; //float f = 12.0000000f; str.Format(_T("%.2g"), f); TRACE(_T("%s\n"), str); Because I've used %g (instead of %f), 12.0 will be 12. Numerics: Take a look at this: http://www.codeproject.com/cpp/floatutils.asp[^]


              - Dy

              J Offline
              J Offline
              Johpoke
              wrote on last edited by
              #6

              Now, that seems good, but it does lots of weird things that i dont want, try setting f to, 299.99 or 31.2699 thanks!!

              /Johannes

              T 1 Reply Last reply
              0
              • J Johpoke

                Now, that seems good, but it does lots of weird things that i dont want, try setting f to, 299.99 or 31.2699 thanks!!

                /Johannes

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                that's normal floating point behavior... know that their height storage capability is at the cost of the precision


                [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

                J 1 Reply Last reply
                0
                • T toxcct

                  that's normal floating point behavior... know that their height storage capability is at the cost of the precision


                  [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

                  J Offline
                  J Offline
                  Johpoke
                  wrote on last edited by
                  #8

                  hu? im like a total beginner at c++

                  /Johannes

                  1 Reply Last reply
                  0
                  • J Johpoke

                    Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!

                    /Johannes

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #9

                    Well, that's not very "generic" (what happens when you need to round at a higher precision?).

                    double Rounder(double fVal, int nPrecision)
                    {
                    nPrecision = __max(__min(16, nPrecision), 1);
                    double fEpsilon = pow(10, nPrecision);
                    double fResult = ( floor(fVal * fEpsilon + 0.5) / fEpsilon);
                    }

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    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