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. Is there any CInt function - VB in c++?

Is there any CInt function - VB in c++?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
23 Posts 7 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 Joy Anne

    Dear c++ friends, In VB we have, Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346. Is there anything in c++ similar to CInt in VB? Thanks, Joy Anne

    S Offline
    S Offline
    Stephen Hewitt
    wrote on last edited by
    #14

    Basic maths will do the trick. Try this: double d = 2345.5678; int i = d + 0.5 Steve

    1 Reply Last reply
    0
    • K kiran janaswamy

      hi, use reinterpret_cast value; snippet of code. int x; double y = 5.66; now x = reinterpret_cast y; // cast double to int data type. this is type conversion in c++. good luck, uday. uday kiran -- modified at 0:57 Wednesday 10th May, 2006

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #15

      You don't need to use reinterpret_cast in this case and in fact shouldn’t: casting from double to int is implicit so the following will do the trick: double d = 3.5; int i = d; // Ok, d=3 Is you wanted to make the cast explicit (and there is good reason to do so as it will suppress a warning) static_cast is the right cast for the job. i.e. double d = 3.5; int i = static_cast<int>(d); Part of the point of adding the function style casts was to make your intent specific thus it is important to use the right cast for the job. Steve

      1 Reply Last reply
      0
      • M Maxwell Chen

        Joy Anne wrote:

        Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346.

        double myD = 2345.5678; int myI = (int)myD;


        Maxwell Chen

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #16

        This will not round. You need something like this: double myD = 2345.5678; int myI = static_cast<int>(myD+0.5); Also note that I didn't use any C-style casts; they should never be used in C++ code. Steve

        S M 2 Replies Last reply
        0
        • S Stephen Hewitt

          This will not round. You need something like this: double myD = 2345.5678; int myI = static_cast<int>(myD+0.5); Also note that I didn't use any C-style casts; they should never be used in C++ code. Steve

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #17

          Which part did my 1-voter not like: The add 0.5 bit or the static_cast lecture? I stand by both of them but I believe that if you're going to down vote something in the programming forums it's good to reply and explain your objection. Steve

          M 1 Reply Last reply
          0
          • S Stephen Hewitt

            Which part did my 1-voter not like: The add 0.5 bit or the static_cast lecture? I stand by both of them but I believe that if you're going to down vote something in the programming forums it's good to reply and explain your objection. Steve

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #18

            I voted 5 to balance it... :-D It used to happen on myself also, when someone doesn't feel like to see the truth. :doh: That's also the reason I posted some reply titled "WTF ?!", remember?! :-D


            Maxwell Chen

            1 Reply Last reply
            0
            • S Stephen Hewitt

              This will not round. You need something like this: double myD = 2345.5678; int myI = static_cast<int>(myD+0.5); Also note that I didn't use any C-style casts; they should never be used in C++ code. Steve

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #19

              Stephen Hewitt wrote:

              any C-style casts; they should never be used in C++ code.

              Personally I guess that the 1-voter doesn't feel comfortable with the sentence above.


              Maxwell Chen

              S 1 Reply Last reply
              0
              • M Maxwell Chen

                Joy Anne wrote:

                Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346.

                double myD = 2345.5678; int myI = (int)myD;


                Maxwell Chen

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #20

                So what's happening?! You guys like to vote 1 very much??


                Maxwell Chen

                1 Reply Last reply
                0
                • M Maxwell Chen

                  Stephen Hewitt wrote:

                  any C-style casts; they should never be used in C++ code.

                  Personally I guess that the 1-voter doesn't feel comfortable with the sentence above.


                  Maxwell Chen

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #21

                  I would guess so - nevertheless they shouldn't be used. Here are some reasons: - A wrong "bad" can cause havoc yet, if C-style casts are used you can’t “grep” the source code for them. - There are many distinct reasons to cast. For example, one is to remove const-ness; another is to “down-cast” in a class hierarchy. With C-style casts all casts look the same and so you have to guess at the intent. Function style casts are explicit and self documenting in this respect. - With C-style casts a simple mistake can change the type of cast and result in unintentional behaviour. For example if your casting to remove const-ness and then you change the type you’re casting it can changes into a “reinterpret” cast. With function style casts the compiler makes sure you can’t cast “more” then you should. i.e. a const_cast can only remove const-ness and not change the type. - Casting is ugly – a well designed program shouldn’t have any – or at most only a few in the lowest level of a system. Casts should be ugly as design errors should be visible. I could go on. There are just so many problems it just not funny. Steve

                  M 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    I would guess so - nevertheless they shouldn't be used. Here are some reasons: - A wrong "bad" can cause havoc yet, if C-style casts are used you can’t “grep” the source code for them. - There are many distinct reasons to cast. For example, one is to remove const-ness; another is to “down-cast” in a class hierarchy. With C-style casts all casts look the same and so you have to guess at the intent. Function style casts are explicit and self documenting in this respect. - With C-style casts a simple mistake can change the type of cast and result in unintentional behaviour. For example if your casting to remove const-ness and then you change the type you’re casting it can changes into a “reinterpret” cast. With function style casts the compiler makes sure you can’t cast “more” then you should. i.e. a const_cast can only remove const-ness and not change the type. - Casting is ugly – a well designed program shouldn’t have any – or at most only a few in the lowest level of a system. Casts should be ugly as design errors should be visible. I could go on. There are just so many problems it just not funny. Steve

                    M Offline
                    M Offline
                    Maxwell Chen
                    wrote on last edited by
                    #22

                    Stephen Hewitt wrote:

                    I could go on. There are just so many problems it just not funny.

                    What did you mean by that?


                    Maxwell Chen

                    S 1 Reply Last reply
                    0
                    • M Maxwell Chen

                      Stephen Hewitt wrote:

                      I could go on. There are just so many problems it just not funny.

                      What did you mean by that?


                      Maxwell Chen

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #23

                      Only that I could have made the list of problems with C-style casts longer. Steve

                      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