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: AustraliaPosts: 57 |
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 Lee |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5914 |
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 MMBasic Help |
||||
Lee3 Regular Member Joined: 17/09/2014 Location: AustraliaPosts: 57 |
Thanks Jim will try it out tonight. Lee |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1328 |
@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: AustraliaPosts: 5914 |
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 MMBasic Help |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1328 |
I remembered that Jim but didn't like to mention it. |
||||
Lee3 Regular Member Joined: 17/09/2014 Location: AustraliaPosts: 57 |
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 |