Question:
what do we use instead of % for FLOAT in C++? as the % is only used for INT. thanks?
rg90
2008-11-22 15:37:09 UTC
im trying to divide a amount of two decimals into Euros and Cents...
ive done the Euros bit.. the only bit left is the Cents..
it doesnt let me do for example:
remainder=amount%0.50
it says this as error: invalid operands of types `float' and `double' to binary `operator%'

So what can i use to get the remainder of the decimals?
i just need something to replace the % .. thanks a lot
Three answers:
Tizio 008
2008-11-22 16:12:28 UTC
A%B means: give me the remainder of the division A/B; a remainder makes sense iff your division is an integer division

(5/2 = 2, 1 as remainder; BUT: 5/2=2.5, no remainder at all);



what you are asking here is: take the integer number N so that N*0.50 is "only X far" from amount; X is your "remainder".



you can easily do it by hand:



int idiv = (int)(amount/0.50); //floored, not ceiled; here hopely

// truncated :)

float frem = amount - (idiv*0.50);
anonymous
2008-11-22 15:42:27 UTC
The % operator has no float equivalent. Use the library function fmod instead in math.h. You might find modf useful too.
anonymous
2008-11-22 15:44:55 UTC
Have you fought of doing calculations in cents.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...