• Welcome to Valhalla Legends Archive.
 

Floating point integer

Started by warz, March 03, 2006, 06:33 PM

Previous topic - Next topic

warz

I'm going to have a decimal in a number of mine, and then multiply it by 100. (Finding percentages)


sprintf(szGlobalBuff, "%1d kb (%f percent free)", free, free/total);


the number that total holds is larger than the number that free holds, so it will result in a decimal. with the %f flag, for floating point, it popups an error saying floating point not loaded. ok? if i do %d, it simply prints 0. whys this acting like the result is 0?

Eric

Declare a seperate floating point variable to hold the result of free/total and then display the result using %f.

K

Quote from: Lord[nK] on March 03, 2006, 06:50 PM
Declare a seperate floating point variable to hold the result of free/total and then display the result using %f.

Or cast one of the variables to a float inline like so:


(float)free / total


the result of an arithmatic operation is the "wider" of the two types involved.  Since you're dividing an integer by an integer, the result will end up as an integer.  Casting one variable to a float results in the end result of the expression being a float.

warz