Number to String (Alternate)

Routine Summary

Converts a positive integer to a string.


N - the number you want to convert


Str1 - the number N in string form

Variables Used

X, N, Ans, Str1

Calculator Compatibility

TI-83+/84+ series



Although there is another number to string routine available, it is somewhat difficult to understand, and also uses three additional variables (two lists and a function variable). This routine, on the other hand, is straightforward and doesn't leave any cleanup afterwards.

With our number stored in N, we loop through each digit of N and store the numeric character to our string that is at the matching position in our substring. You access the individual digit in the number by using iPart(10fPart(A/10^(X, and then locate where it is in the string "0123456789". The reason you need to add 1 and not(N) is so that it works with the 0 digit.

In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used for. Each time through the For( loop, we concatenate the string from before (which is still stored in the Ans variable) to the next numeric character that is found in N. Using Ans allows us to not have to use another string variable, since Ans can act like a string and it gets updated accordingly, and Ans is also faster than a string variable.

By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy character to the string initially, we now need to remove it, which we do by getting the substring from the first character to the second to last character of the string. Finally, we store the string to a more permanent variable (in this case, Str1) for future use.

The way we get the number of digits in a number is by using the 1+int(log( trick. This trick works with any positive whole numbers, and if you add abs( after log(, it will also work with negative numbers. Unfortunately, it does not work with decimals. Also note that we are looping through the digits backwards, and concatenate the previous digit to the next digit. This is done purely for optimization sake.

Reversed version

When the string is being concatenated to one that already exists, this code can be optimized by decrementing the For loop rather than incrementing, and appending each character rather than prepending.

:[your string here]

Related Routines


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.