...

View Full Version : Convert decimal values to integers



Advancedkill
08-02-2009, 01:02 AM
Hi

If I want to assign the value 3 to an integer variable by doing 10/3 (and forgetting about the remainder) how would I do this? (Using C for my language)

Would


int x = 10/3;

always work?

Thanks

BrickInTheWall
08-02-2009, 01:10 AM
yes that works. Look up type casting for C/C++ to see what can be cast implicitly and what requires explicit conversions.

oracleguy
08-02-2009, 01:11 AM
Yes, but if you want to be sure you could floor (http://www.cplusplus.com/reference/clibrary/cmath/floor/) function. (I'm assuming that you aren't going to be assigning a constant that way, that the input is actually going to be dynamic.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum