Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 49 / 68

Formatting numbers (aligning digits and decimal places)

This lesson covers formatting numbers in Julia, with the aim of displaying them in a readable form.

Loading the standard Printf module with using Printf gives you @printf("%.2f", value) to fix the number of decimal places and @printf("%03d", value) to pad the front with zeros. Both are handy for displaying amounts and for building numbered file names.

The sample code turns 1234.5 into two-decimal form with @printf("%.2f ", price) and 7 into the zero-padded "007" with @printf("%03d ", num). Note that these are macros.

A common early stumble is forgetting the using Printf import. Unlike most features, formatted output needs the Printf module loaded explicitly.

In professional work, results displayed raw have inconsistent widths and read badly, so this kind of formatting just before display is standard.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)