Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

casting

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do cast a float number( 1.234) to an integer (1)???

like
a=(int)1.234
in C/C++
 
One more thing, all this is caused by the function rand, I need it to return me an integer random number, and I get a float number.

So if there is anyway to random an integer, please tell me.
 
Hi dafoki,

You nearly had it by just copying the C++ code actually, try:

a=int(1.234);


Mike
michael.j.lacey@ntlworld.com
 
[tt]
$a = int ((rand) * 100);
[/tt]
will give you an integer between 0 and 99.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
$a = int ((rand) * 100) + 1;

Will give you an integer between 0 and 100.

Just thought you might like to know that.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
additionally,
be aware that there is no 'real' casting in Perl like there is in other languages that type their vars. Once you have an integer in $a, there is nothing to keep you from sticking a float in again.

$a = int(3.2); # now $a is 3
$a = $a/2; # now $a is 1.5





keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top