Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:51 03 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Forcing leading zeros

Author Message
Lee3
Regular Member

Joined: 17/09/2014
Location: Australia
Posts: 57
Posted: 01:25pm 10 Dec 2014
Copy link to clipboard 
Print this post

I've written some code that adds a number of minutes to the current time, and outputs a new time. This is on a umite.
e.g 23:00 + 125 minutes = 01:05
instead in this case what I would get is 1:5

is there a way of forcing a leading zero?

I could probably do something with if/then/length operators to decide if the number has 1 or 2 digits, and add a zero, but I suspect there is a much more elegant way ahead.... :)

Cheers
LeeEdited by Lee3 2014-12-11
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6266
Posted: 05:06pm 10 Dec 2014
Copy link to clipboard 
Print this post

For the micromite, STR$ will do what you want:
h=1
m=5
s=0
myTime$=STR$(h,2,0,"0")+":"+STR$(m,2,0,"0")+":"+STR$(s,2,0," 0")

print myTime$


Jim
VK7JH
MMedit
 
Lee3
Regular Member

Joined: 17/09/2014
Location: Australia
Posts: 57
Posted: 05:40pm 10 Dec 2014
Copy link to clipboard 
Print this post

Thanks Jim
will try it out tonight.
Lee
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 08:34pm 10 Dec 2014
Copy link to clipboard 
Print this post

@Lee3,

Be careful to remove that extra space that the board has dropped into Jim's "mytime$=... line there Lee - near the end. The last expression doesn't contain a space, it should be +STR$(s,2,0,"0"). It's an ongoing problem that the bulletin board has whenever a long string is posted without any spaces.

Greg
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6266
Posted: 08:45pm 10 Dec 2014
Copy link to clipboard 
Print this post

And it was only the other day that I pointed out that you can ofter get arround the extra space problem by putting your own spaces where they don't cause problems:

h=1
m=5
s=0
myTime$=STR$(h,2,0,"0")+":"+ STR$(m,2,0,"0")+":"+ STR$(s,2,0,"0")

print myTime$


I should pay attention to myself more often.

Jim
VK7JH
MMedit
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 09:16pm 10 Dec 2014
Copy link to clipboard 
Print this post

I remembered that Jim but didn't like to mention it.
 
Lee3
Regular Member

Joined: 17/09/2014
Location: Australia
Posts: 57
Posted: 09:18pm 10 Dec 2014
Copy link to clipboard 
Print this post

I saw that space, wasn't going to put it in.... :)
Then I saw in the manual this is all spelled out....
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025