Is there any CInt function - VB in c++?
-
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
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
-
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
-
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
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
-
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
-
I think he is trying to convert a double to int.
Maxwell Chen
-
I think he is trying to convert a double to int.
Maxwell Chen
-
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
Math.round public static int round( float a ) Returns the value of the argument rounded to the nearest int value. -------------------------- just do it like that : #include : double MyDouble = 2345.5678 // MyDouble is a Double. int MyInt = ROUND(MyDouble) // MyInt contains 2346. : double MyDouble = 2345.4678 // MyDouble is a Double. int MyInt = ROUND(MyDouble) // MyInt contains 2345.
-
Nibu thomas wrote:
Faqs are worth a read
:-D
Maxwell Chen
-
Math.round public static int round( float a ) Returns the value of the argument rounded to the nearest int value. -------------------------- just do it like that : #include : double MyDouble = 2345.5678 // MyDouble is a Double. int MyInt = ROUND(MyDouble) // MyInt contains 2346. : double MyDouble = 2345.4678 // MyDouble is a Double. int MyInt = ROUND(MyDouble) // MyInt contains 2345.
csc wrote:
public static int round( float a )
Is this in C++ ? I mean that
public
modifier.csc wrote:
int round( float a )
csc wrote:
ROUND(MyDouble)
Case sensitive?! :wtf: No '
;
' , it doesn't compile. :~
Maxwell Chen
-
Nibu thomas wrote:
Faqs are worth a read
:-D
Maxwell Chen
-
Nibu thomas wrote:
I can see that question coming!
The diviner ~~~ :laugh:
Maxwell Chen
-
csc wrote:
public static int round( float a )
Is this in C++ ? I mean that
public
modifier.csc wrote:
int round( float a )
csc wrote:
ROUND(MyDouble)
Case sensitive?! :wtf: No '
;
' , it doesn't compile. :~
Maxwell Chen
yes maybe in next time,isnt possible!?
-
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
Basic maths will do the trick. Try this:
double d = 2345.5678; int i = d + 0.5
Steve -
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
You don't need to use
reinterpret_cast
in this case and in fact shouldn’t: casting fromdouble
toint
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 -
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
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 -
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. SteveWhich 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 -
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. SteveI 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
-
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. SteveStephen 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
-
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
So what's happening?! You guys like to vote 1 very much??
Maxwell Chen