(newbie question) How can I truncate an integer?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

(newbie question) How can I truncate an integer?

Post 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.
fennec
Posts: 55
Joined: Fri Oct 10, 2008 7:23 am

Post 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.
Last edited by fennec on Mon Nov 10, 2008 8:42 am, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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.
Image Image Image
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

ps

Post by jhend60 »

i need to get the value into seconds
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

And? What's the problem?

Show us your code and explain the problem.
Image Image Image
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

kk

Post 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?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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?
Image Image Image
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

huh

Post 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...
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post 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.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

You could start out with a float, do your math on it, then cast it down to an int for drawing purposes.
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post 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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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);
Image Image Image
Post Reply