Convert a floating point number to an integer. This function returns the integral part of the number and does roughly correspond to the C++ function "floor()". When in doubt, round the float values before conversion.
Name | Type | Default | Description |
Return | int | f as integer | |
f | float | - | number to convert |
int main() { // toint(2.9) returns 2 showmessage("%d", toint(2.9)); // toint(-2.9) returns -2 showmessage("%d", toint(-2.9));
return 0; }
Der Datentyp int ist 64 Bit breit. Hier ein Beispiel mit Zahlen, deren Darstellung mehr als 32 Bit benötigt.
int main () { int a = 6917529027641081856; char * astr = "6917529027641081856";
wlog ("", "a=%d\t(and as string '%s')\n", a, itoa (a)); wlog ("", "a=0x%x\t(and as string '%s')\n", a, itoa (a)); wlog ("", "astr='%s'\t(and as int %d)\n", astr, val (astr)); wlog ("", "astr='%s'\t(and as int 0x%x)\n", astr, val (astr));
wlog ("", "string compare : a ?= astr : %d\n", (a == val (astr))); wlog ("", "int compare : a ?= astr : %d\n", strcmp (itoa (a), astr)==0);
return 0; } // Ausgabe // //a=6917529027641081856 (and as string '6917529027641081856') //a=0x6000000000000000 (and as string '6917529027641081856') //astr='6917529027641081856' (and as int 6917529027641081856) //astr='6917529027641081856' (and as int 0x6000000000000000) //string compare : a ?= astr : 1 //int compare : a ?= astr : 1
Das obige Beispiel funktioniert natürlich auch dann, wenn die Zahlen als Hex-Zahlen definiert werden.
int main () { int a = 0x6000000000000000; char * astr = "0x6000000000000000";
wlog ("", "a=%d\t(and as string '%s')\n", a, itoa (a)); wlog ("", "a=0x%x\t(and as string '%s')\n", a, itoa (a)); wlog ("", "astr='%s'\t(and as int %d)\n", astr, val (astr)); wlog ("", "astr='%s'\t(and as int 0x%x)\n", astr, val (astr));
wlog ("", "string compare : a ?= astr : %d\n", (a == val (astr))); wlog ("", "int compare : a ?= astr : %d\n", strcmp (itoa (a), astr)==0);
return 0; } // Ausgabe // //a=6917529027641081856 (and as string '6917529027641081856') //a=0x6000000000000000 (and as string '6917529027641081856') //astr='0x6000000000000000' (and as int 6917529027641081856) //astr='0x6000000000000000' (and as int 0x6000000000000000) //string compare : a ?= astr : 1 //int compare : a ?= astr : 1
Alphabetic index HTML hierarchy of classes or Java