The FORMAT$() function converts a format string and a list of values into a single string. (Nearly identical to sprintf() in C)
Rather than using multiple STR$, HEX$, or BIN$ functions, and combining strings, you can use FORMAT$ to make your code neater.
For example, if you're displaying a list of names and highscores, and you want the rows to line up:
GPUTCHR 0,0," "*(8-LEN(NAME$[I]))+NAME$[I]+" : "+STR$(HIGHSCORE[I])
GPUTCHR 0,0,FORMAT$("%8S : %D",NAME$[I],HIGHSCORE[I])
FORMAT$ can accept an unlimited* number of values (even 0!).
string$ = FORMAT$(formatString$, values ... )formatString$ is a string containing normal text and formatting codes.
Formatting codes start with % and end with a letter that controls the type. They can also have optional flags between the % and the letter.
"%" can be inserted into the output by escaping it: %%
% [ flags ] [ length ] [ . precision ] type
S - string
D - decimal integer
X - hexadecimal integer
B - binary integer
F - floating point
Not case sensitive.
Values are truncated (rounded towards 0) when displayed as an integer.
Optional. The minimum length of the inserted value.
By default, space padding will be added to the left side if the value is too short.
- adds a space before positive numbers. (Only works with %D and %F.) (ignored if the + flag is present)
+ - adds a + sign before positive numbers. (Only works with %D and %F.)
- - adds padding to the right side instead of the left side.
0 - uses 0s for padding instead of spaces (unless the - flag is present or if the value is inf or nan).
Optional (defaults to 6). (Only works with %F and %S)
Numbers will be rounded to this number of decimal places. If the last digit has only a 5 after it, it will be rounded unpredictably.
Using 0 will not display the decimal point, and rounds numbers ending in .5 to the nearest even number.
In %S, the precision setting behaves differently. It will truncate a string to that length if it is longer (rather than extending it like length does).
You can force a string to be a certain length by setting both length and precision using the format code %n.nS where n is the length you want.