background image
Original Author Paul Laughton, 2011
Page 71
De Re BASIC!
Formatter class, or the format(String, Object...) method of the Java String, with two exceptions: Boolean
format specifiers are not supported, and hex hash specifiers are limited to numeric and string types.
If you have not programmed in one of those other languages, this will be your introduction to a
powerful tool for formatting text.
A format expression is a string with embedded format specifiers. Anything that is not a format specifier
is copied literally to the function output string. Each embedded format specifier is replaced with the
value of an expression from the list, formatted according to the specifier. For example:
PRINT USING$("","Pi is approximately %f.", PI())
function call
Pi is approximately 3.141593.
printed output for English locale
The <locale_exp> is "", meaning "use my default locale".
The <format_exp>, "Pi is approximately %f", has one format specifier, "%f".
"%f" means, "use the default decimal floating point output format".
The expression list has one item, the math function PI().
In the output, "%f" is replaced by the value of the the PI() function.
Your output may be different if your locale language is not English.
Format Specifiers
Here is a brief summary of the available format specifiers:
For this type of data
Use these formats
Comments
String
%s %S
%S forces output to upper-case
Number
%f %e %E %g %G %a %A
Standard BASIC! numbers are floating point
Use %f for decimal output: "1234.567"
Use %e or %E for exponential notation: "1.234e+03"
%E writes upper-case: "1.234E+03"
%g (%G) lets the system choose %f or %e (%E)
%a and %A are "hexadecimal floating point"
Integer
%d %o %x %X
USING$ can use some math functions as integers
Use %d for decimal, %o for octal, %x %X for hex
%x writes lower-case abcdef, %X writes upper-case
Special integer
%c %C %t
These specifiers can operate on an integer
%c %C output a character, %C writes upper-case
%t represents a family of time format specifiers
None
%% %n
These specifiers do not read the expression list
%% writes a single "%" to the output
%n writes a newline, exactly the same as \n
For more information about %a and %A, see the Android documentation linked above.
Android's %b and %B are not supported because BASIC! has no Boolean type.
Android's %h and %H hash code specifiers are limited to strings and numbers in BASIC!.
For an explanation of USING$() with integer format specifiers, see below.