S
Simon
Hi all
I am writing a small app that uses real numbers all over the place for
calculations.
But when it comes to displaying values it is better to only display an it,
(especially when it comes to % values and so on).
so I first thought that the best way would be...
....
double x = 1.23456789012345;
int y = (int)x;
but that does not always work, in fact when the double has a lot of decimal
places I get a rather nasty looking number.
I am assuming that the casting could get it wrong if there are too many
decimal places.
so I thought I'd use the ceil(...); and floor(...); functions
....
y = (x>=0)?(int((floor(x))) : (int(ceil(x))));
the logic been that the decimal places are removed by both functions and the
casting then works just fine.
but that raises a few questions.
Why does the cast not work some times? is the floor/ceil method "guaranteed"
to work? What would be the preferred/best method?
Many thanks in advance.
Simon
I am writing a small app that uses real numbers all over the place for
calculations.
But when it comes to displaying values it is better to only display an it,
(especially when it comes to % values and so on).
so I first thought that the best way would be...
....
double x = 1.23456789012345;
int y = (int)x;
but that does not always work, in fact when the double has a lot of decimal
places I get a rather nasty looking number.
I am assuming that the casting could get it wrong if there are too many
decimal places.
so I thought I'd use the ceil(...); and floor(...); functions
....
y = (x>=0)?(int((floor(x))) : (int(ceil(x))));
the logic been that the decimal places are removed by both functions and the
casting then works just fine.
but that raises a few questions.
Why does the cast not work some times? is the floor/ceil method "guaranteed"
to work? What would be the preferred/best method?
Many thanks in advance.
Simon