In some languages, integer literals may contain digit separators to allow
digit grouping into more legible forms. If this is available, it can usually be done for floating point literals as well. This is particularly useful for
bit fields and makes it easier to see the size of large numbers (such as a million) at a glance by
subitizing rather than counting digits. It is also useful for numbers that are typically grouped, such as
credit card number or
social security numbers. Very long numbers can be further grouped by doubling up separators. Typically decimal numbers (base-10) are grouped in three digit groups (representing one of 1000 possible values), binary numbers (base-2) in four digit groups (one
nibble, representing one of 16 possible values), and hexadecimal numbers (base-16) in two digit groups (each digit is one nibble, so two digits are one
byte, representing one of 256 possible values). Numbers from other systems (such as id numbers) are grouped following whatever convention is in use.
Examples In
Ada,
C# (from version 7.0),
D,
Eiffel,
Go (from version 1.13),
Haskell (from GHC version 8.6.1),
Java (from version 7),
Julia,
Perl,
Python (from version 3.6),
Ruby,
Rust and
Swift, integer literals and float literals can be separated with an
underscore (_). There can be some restrictions on placement; for example, in Java they cannot appear at the start or end of the literal, nor next to a decimal point. While the period, comma, and (thin) spaces are used in normal writing for digit separation, these conflict with their existing use in programming languages as
radix point, list separator (and in C/C++, the
comma operator), and token separator. Examples include: int oneMillion = 1_000_000; int creditCardNumber = 1234_5678_9012_3456; int socialSecurityNumber = 123_45_6789; In
C++14 (2014) and the next version of
C ,
C23, the apostrophe character may be used to separate digits arbitrarily in numeric literals. The underscore was initially proposed, with an initial proposal in 1993, and again for
C++11, following other languages. However, this caused conflict with
user-defined literals, so the apostrophe was proposed instead, as an "
upper comma" (which is used in some other contexts). auto integer_literal = 1'000'000; auto binary_literal = 0b0100'1100'0110; auto very_long_binary_literal = 0b0000'0001'0010'0011''0100'0101'0110'0111; ==Notes==