Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 49 / 68

Formatting numbers (aligning digits and decimal places)

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

ToString("F2") shows two decimal places and ToString("D3") zero-pads a number to three digits. Both are format specifiers for displaying amounts and numbers at a fixed width.

The sample code turns 1234.5 into the two-decimal "1234.50" with price.ToString("F2") and 7 into the zero-padded "007" with num.ToString("D3"). Note the difference between the F and D specifiers.

A common early stumble is the sheer number of specifiers. F (fixed point), D (zero-padded integer), N (thousands separators) and more all exist, and you have to pick the one that suits.

In professional work, number formatting is used very frequently on any screen shown to a user — thousands separators on amounts, consistent decimal places, zero-padded order numbers.

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

๐Ÿงช This site can't compile or run VB.NET 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)