LoginLogin

FORMAT$ guide

Root / Submissions / [.]

12Me21Created:
The FORMAT$() function converts a format string and a list of values into a single string. (Nearly identical to sprintf() in C, but with some of the formatting codes blocked) 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])
'can be...
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: %%

Arguments:

% [ flags ] [ length ] [ . precision ] type

Type:

Required. 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. When converting values to integers, FORMAT$ will throw an overflow error if the value is too big or too small.

Length:

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.

Flags:

Optional. - 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).

Precision:

Optional. (Only works with %F and %S) Numbers will be rounded to this number of decimal places. If precision is omitted, %F defaults to 6 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.

Shoot, thanks so much! I was legit about to make a question post about this.

This is very useful! I didn't even know it was this cool. Thanks!