writing variable as 01 instead of 1

Started by cmunoz, June 25, 2014, 10:56:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

cmunoz

Hello everybody. This seems like a simple problem, but I haven't been able to get it to work. Basically what I want to do is write a variable to the screen so that if the number is less than two digits it displays a 0 in the tens place.


int myvariable=3;

write(0,160,120,4,(myvariable));


This results in the number "3" printed on screen. But what I want is for the number "03" to be printed on screen. I've tried various things such as myvariable+00 but I haven't had much luck.


I suppose I could do it like this....



if(myvariable<10)

write(0,160,120,4,"0"+(myvariable));
else
write(0,160,120,4,(myvariable));
end


But I have the feeling there's a much tidier way of going about it since that would get a bit sloppy the more variables are added.
Any ideas?

Breadcaster

Yeah I actually have a similar issue, I've got an ingame timer which is written on screen and I'd ideally like it to be written as ##:## (e.g. "03:25" rather than "3:25"), it just looks sorta... well, it serves its function but it just doesn't LOOK right. Is there any way to change/fix this?

Drumpi

As far as I know, no, that's the best way I think you can do it.
For a timer, you can write the numbers from right to left, and use three WRITE functions (remember to save the write ID to use with DELETE_TEXT)... or you can use a FPG with 10 graphs with every digit, and four process.
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

Breadcaster

Best way on this, I guess, is to have a statement like:

if (timer[0]>60);
    seconds++;
    timer[0]=0;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end

??

Drumpi

By the way, just check if timer is working OK. Last time I checked with a VERY OLD version of Bennu, timers aren't as accurate as a manual counter.
And maybe your code must be:

if (timer[0]>60);
    seconds++;
    timer[0] -= 60;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end


Or else you will be deprecating some hundredths seconds every 60 (timer is in hundredts of seconds, not seconds). Check the doc:
http://wiki.bennugd.org/index.php?title=Timer
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

SplinterGU

take a look at this


import "mod_say"
import "mod_string"

global
    int
    a;
begin

    a = 1;
    say(substr("0"+a, -2, 2));

    a = 4;
    say(substr("0"+a, -2, 2));

    a = 5;
    say(substr("0"+a, -2, 2));

    a = 12;
    say(substr("0"+a, -2, 2));

end
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Breadcaster

Quote from: Drumpi on June 15, 2016, 07:14:24 PM
By the way, just check if timer is working OK. Last time I checked with a VERY OLD version of Bennu, timers aren't as accurate as a manual counter.
And maybe your code must be:

if (timer[0]>60);
    seconds++;
    timer[0] -= 60;
end
if (seconds<10);
    write(fontname, 320, 240, 4, "0"+itoa(seconds));
else
    write_int(fontname, 320, 240, 4, &seconds);
end


Or else you will be deprecating some hundredths seconds every 60 (timer is in hundredts of seconds, not seconds). Check the doc:
http://wiki.bennugd.org/index.php?title=Timer

Thank you so much Drumpi, this actually fixed a bug I've been having for quite a while!! I'm using a newer version of Bennu but this still proved useful.

Drumpi

Ok.
But be carefull, don't use write in one line and write_int in the next, jus t pick one (write is the best option, because you're gonna use delete_text in the next frame).
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)