? FORMAT$ guide ● SmileBASIC Source

Sign In

*Usernames are case-sensitive
Forgot my password

Migrating to a new website for SmileBASIC 4

FORMAT$ guide

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: %%


% [ flags ] [ length ] [ . precision ] 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.


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.


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


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.
9 votes
2 Comment(s) RaichuBender RaichuBender Avatar Taboo I didn't change my avatar for 180 days Website Avatar Embargo I didn't change my avatar for 90 days Website Avatar Block I didn't change my avatar for 30 days. Website This is very useful! I didn't even know it was this cool. Thanks! MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies Shoot, thanks so much! I was legit about to make a question post about this.