string always 2 numbers 01 02

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

string always 2 numbers 01 02

Post by stefan.erni »

I have to convert an integer to a string. The integer has a value from 1 to 12.

The string needs always 2 numbers

result_string = ToString$ (1) // the result_string is "1" but I need "01"

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: string always 2 numbers 01 02

Post by Benj »

Hello,

You can simply check the length of the string and then add the leading 0 if required.

Calculation: length = Length$(result_string)
Decision: length < 2
Yes: result_string = "0" + result_string

Post Reply