(newbie question) How can I truncate an integer?
(newbie question) How can I truncate an integer?
I know this isn't relevant to irrlicht, but I think this would be the easiest way to find an answer. 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.
Thanks.
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.
int time = 199 / 100 ; //reads 19
I'm sure someone will be in shortly to show a better way to do it.
Last edited by fennec on Mon Nov 10, 2008 8:42 am, edited 1 time in total.
ps
i need to get the value into seconds
kk
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?
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?
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?
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
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...
-
- Posts: 368
- Joined: Tue Aug 21, 2007 1:43 am
- Location: The Middle of Nowhere
-
- Posts: 126
- Joined: Sun Apr 02, 2006 1:21 am
- Location: Canada
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.
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.