Page 1 of 1

(newbie question) How can I truncate an integer?

Posted: Mon Nov 10, 2008 8:32 am
by jhend60
I know this isn't relevant to irrlicht, but I think this would be the easiest way to find an answer. :roll: Ok. I have made an integer (2000) and I have set it to count down by 1 every frame. This gives 20 seconds before it reaches 0. I need to make a static text showing time left. I have divided the integer by 100 to get its value, but it will have decimals (bad). What is the easiest way to truncate a integer, and how will I do it?

Thanks.

Posted: Mon Nov 10, 2008 8:40 am
by fennec
Create a new integer variable, and assign it that value. It'll have to truncate it.

int time = 199 / 100 ; //reads 19


I'm sure someone will be in shortly to show a better way to do it.

Posted: Mon Nov 10, 2008 8:42 am
by JP
If you divide an integer by 100 then you don't get any decimals... it returns an integer...

If you're getting decimals in your static text then you're simply putting the value into your text in the wrong way.

ps

Posted: Mon Nov 10, 2008 9:18 am
by jhend60
i need to get the value into seconds

Posted: Mon Nov 10, 2008 9:25 am
by JP
And? What's the problem?

Show us your code and explain the problem.

kk

Posted: Mon Nov 10, 2008 9:37 am
by jhend60
I have this code:
timeleftint = timeleftint / 100;
swprintf(countdown, 256, L" %i seconds left", timeleftint);

timeleft = guienv->addStaticText(countdown,rect<s32>(900,500,1024,600),false,true,0,-1,false);
timeleft->draw();

it should work, but it always shows as '0'.
If i remove timeleftint = timeleftint / 100;, it works fine but counts down from 2500. is there something I am doing wrong?

Posted: Mon Nov 10, 2008 9:47 am
by JP
Not sure why you're calling timeleft->draw()... that shouldn't be necessary, if you're calling guienv->drawAll() in your draw loop...

And you should divide by 1000 to go from milliseconds to seconds, but that won't be causing the problem.

I can't think why it's not working... though, must be something i'm missing... Presumably timeleftint is indeed an integer....

Seeing as i can't see a problem i'll suggest something that might work:

f32 timeleft = 2500;
timeleft = timeleft / 100.0f;
swprintf(countdown, 256, L" %.0f seconds left", timeleft);

The %0.f bit says stick in a float with nothing after the decimal point, so basically gives you an integer from a float.

Presumably you're not adding a new static text to the gui every time round the loop, and you are in some way updating the static text's text yeah?

huh

Posted: Mon Nov 10, 2008 9:52 am
by jhend60
I used the same code as you said, but, again, when I leave the dividing code in it stuffs up. It showed -0.01010 or something like that this time. As soon as I removed the divide part it worked fine... again...

Posted: Mon Nov 10, 2008 10:14 am
by Dark_Kilauea
Take an f32 and convert it to an int (u32 or s32). C++ should automatically truncate it for you.

If not, run floor() on the number to force it to the nearest integer.

Posted: Tue Nov 11, 2008 11:52 am
by DarkDepths
You could start out with a float, do your math on it, then cast it down to an int for drawing purposes.

Posted: Tue Nov 11, 2008 12:14 pm
by Eigen
Hmm .. I don't get what this code is supposed to do .. if you divide your time by 100 each frame you get:

Frame1:
2500
Frame 2:
25
Frame 3:
0,25
Frame 4:
0,125
..

So you get to 0 faster than you can blink depending on your framerate (which is a bad idea to measure time anyway)

How is that supposed to count time? I think you're better off using a timer.

Posted: Tue Nov 11, 2008 12:44 pm
by JP
oh god well done eigen... that must be it! i didn't realise timeleftint was basically global and would be reduced each frame to nothing very quickly!

Simple solution:
timeleftint = timeleftint;
swprintf(countdown, 256, L" %i seconds left", timeleftint/1000);