The print
function is defined in modules/Basic
, and is used to write strings directly to standard output or console.
Here is the function definition:
print :: (format_string: string, args: .. Any) -> bytes_printed: s64;
A %
sign marks the place in which the variable will be printed out at. %1
prints out the first argument, %2
prints out the second argument, %3
prints out the third argument, and so on. %%
will print out a single %
sign.
// prints out "Hello, My name is John Newton"
print("Hello, My name is % %\n", "John", "Newton");
// prints out "Hello, My name is Newton John"
print("Hello, My name is %2 %1\n", "John", "Newton");
// prints out "Congratulations! You scored 100% on the test!"
print("Congratulations! You scored 100%% on the test!\n");
The print function supports internationalization and localization.
print("你好!\n"); // prints hello in Chinese.
Formatting Variables
Just like C, Jai supports formatting variables with functions such as formatFloat
, formatStruct
, and formatInt
. These are defined in modules/Basic/Print.jai
.
v := Vector3.{1.0, 2.0, 3.0};
print("v = %\n", formatStruct(v,
use_long_form_if_more_than_this_many_members=2,
use_newlines_if_long_form=true);
integer := 0xFF;
// prints out the number in hexadecimal
print("i = %\n", formatInt(integer, base=16));
number := 3.14157;
// prints out the float number
print("%\n", formatFloat(number, width=3, trailing_width=3, zero_removal=.NO));