October 2006
Intermediate to advanced
888 pages
16h 55m
English
To output numbers in a specific format, you can use the printf method in the Kernel module. It is virtually identical to its C counterpart. For more information, see the documentation for the printf method.
x = 345.6789
i = 123
printf("x = %6.2f\n", x) # x = 345.68
printf("x = %9.2e\n", x) # x = 3.457e+02
printf("i = %5d\n", i) # i = 123
printf("i = %05d\n", i) # i = 00123
printf("i = %-5d\n", i) # i = 123To store a result in a string rather than printing it immediately, sprintf can be used in much the same way. The following method returns a string:
str = sprintf("%5.1f",x) # "345.7"Finally, the String class has a % method that performs this same task. The % method has a format string as a receiver; it takes ...
Read now
Unlock full access