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 / C++ / MFC
  4. How to round a double value

How to round a double value

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
11 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.
  • K Offline
    K Offline
    King_of_Queens
    wrote on last edited by
    #1

    Hi i search for a function which can do this: double value = 4,6793 value = round(value,2) //after that value should be 4,68 in which library can i find this function???

    L D O 3 Replies Last reply
    0
    • K King_of_Queens

      Hi i search for a function which can do this: double value = 4,6793 value = round(value,2) //after that value should be 4,68 in which library can i find this function???

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      do you mean round to 4,6800? or truncate to 467.93 and round to 468? I don't really get how you can get 4,68? You'll need to make you own round routine though if you want to do anything really weird with numbers

      K 1 Reply Last reply
      0
      • L Lost User

        do you mean round to 4,6800? or truncate to 467.93 and round to 468? I don't really get how you can get 4,68? You'll need to make you own round routine though if you want to do anything really weird with numbers

        K Offline
        K Offline
        King_of_Queens
        wrote on last edited by
        #3

        Hi i mean this: round(y,x) y = value to round for example 4,6793233 x = position after ',' to round round should look after y at position x+1 if y at positon x is > 5 then 4,68 forget the rest of numbers!

        L 1 Reply Last reply
        0
        • K King_of_Queens

          Hi i mean this: round(y,x) y = value to round for example 4,6793233 x = position after ',' to round round should look after y at position x+1 if y at positon x is > 5 then 4,68 forget the rest of numbers!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          OK, I'm not aware of any function in the standard libs that'll do what you want. Also, if the number you specify is a double where does this comma come from? I mean the double number 46793233 would be 46,793,233. So I'm assuming the comma is always after the first digit? Either way, iterating through figures is a bit of a nightmare, and i'm not really sure of the best way to go about it, sorry.

          V 1 Reply Last reply
          0
          • L Lost User

            OK, I'm not aware of any function in the standard libs that'll do what you want. Also, if the number you specify is a double where does this comma come from? I mean the double number 46793233 would be 46,793,233. So I'm assuming the comma is always after the first digit? Either way, iterating through figures is a bit of a nightmare, and i'm not really sure of the best way to go about it, sorry.

            V Offline
            V Offline
            vcplusplus
            wrote on last edited by
            #5

            In some countries they use the ',' intead of the '.'. Example: In America 4.67 In Italy 4,67

            L 1 Reply Last reply
            0
            • K King_of_Queens

              Hi i search for a function which can do this: double value = 4,6793 value = round(value,2) //after that value should be 4,68 in which library can i find this function???

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

              This should work for you: double a = 4.6793; a = a + 0.005; // 4.6843 int b = a * 100.0; // 468 double c = b / 100.0; // 4.68

              L 1 Reply Last reply
              0
              • V vcplusplus

                In some countries they use the ',' intead of the '.'. Example: In America 4.67 In Italy 4,67

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Interesting...why is that? The '.' is a decimal point to indicate decimal figures in a number, so why use a ','? :confused: Cheers for pointing that out to me though, I would never have guessed :)

                Q V 2 Replies Last reply
                0
                • D David Crow

                  This should work for you: double a = 4.6793; a = a + 0.005; // 4.6843 int b = a * 100.0; // 468 double c = b / 100.0; // 4.68

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Now I know what I'm doing, I'd do it like this : //could make it a variable parameter so you don't have to keep supplying //it with a param when you just want a standard round. double rounddouble(double val, int fig=0) { //Avoid division by zero by checking fig for zero if(fig != 0) { //get the multiply divide factor double sigfig = pow(10, (double)fig); //multiply the figure by 10pow to get the pos to chop the end off val *= sigfig; //add 0.5 before rounding so we round up or down val += 0.5; //adding 0.5 affects round down value in effect, rounding up or down. val = floor(val); //return the decimal to original value with chopped off rounded crap val /= sigfig; } else { //just round the figure without an offset val += 0.5; val = floor(val); } //finally return the value return val; } Hope this helps. This will return the correct rounded double value based on however many significant fig's you specify. Alan.;P

                  1 Reply Last reply
                  0
                  • L Lost User

                    Interesting...why is that? The '.' is a decimal point to indicate decimal figures in a number, so why use a ','? :confused: Cheers for pointing that out to me though, I would never have guessed :)

                    Q Offline
                    Q Offline
                    QuiJohn
                    wrote on last edited by
                    #9

                    Dangleberry wrote: Interesting...why is that? The '.' is a decimal point to indicate decimal figures in a number, so why use a ','? Because it's the other way around in a lot of countries. :) It can throw you at first, but it's something to keep in mind while on international forums.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Interesting...why is that? The '.' is a decimal point to indicate decimal figures in a number, so why use a ','? :confused: Cheers for pointing that out to me though, I would never have guessed :)

                      V Offline
                      V Offline
                      vcplusplus
                      wrote on last edited by
                      #10

                      A decimal point is an English notation. Most other countries use decimal commas, and the thousand separator is a period.

                      1 Reply Last reply
                      0
                      • K King_of_Queens

                        Hi i search for a function which can do this: double value = 4,6793 value = round(value,2) //after that value should be 4,68 in which library can i find this function???

                        O Offline
                        O Offline
                        ohadp
                        wrote on last edited by
                        #11

                        I think this short scribble should work: double round(double x, int digits) { double _int = (double)((int)x); double _pow = pow(10, digits); double _fract = (x - _int) * _pow; return _int + (_fract / _pow); }

                        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