Valhalla Legends Forums Archive | C/C++ Programming | Floating point integer

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

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

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?
March 4, 2006, 12:33 AM
LoRd
Declare a seperate floating point variable to hold the result of free/total and then display the result using %f.
March 4, 2006, 12:50 AM
K
[quote author=Lord[nK] link=topic=14418.msg147548#msg147548 date=1141433413]
Declare a seperate floating point variable to hold the result of free/total and then display the result using %f.
[/quote]

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

[code]
(float)free / total
[/code]

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.
March 4, 2006, 1:59 AM
warz
thanks a lot.
March 4, 2006, 2:21 AM

Search