Output Formats for Numbers

Output formats for numbers.

These numbers are given in the default output format. Large numbers are given in scientific notation:

{6.7^-4,6.7^6,6.7^8}

This gives all numbers in scientific notation:

ScientificForm[%]

This gives the numbers in engineering notation, with exponents arranged to be multiples of three:

EngineeringForm[%]

In accounting form, negative numbers are given in parentheses, and scientific notation is never used:

AccountingForm[{5.6,-6.7,10.^7}]

Controlling the printed precision of real numbers.

Here is (π^9) to 30 decimal places:

N[Pi^9,30]

This prints just 10 digits of (π^9):

NumberForm[%,10]

This gives 12 digits, in engineering notation:

EngineeringForm[%,12]

Options for number formatting.

All the options in the table except the last one apply to both integers and approximate real numbers.

All the options can be used in any of the functions (RefLink[NumberForm, paclet:ref/NumberForm]), (RefLink[ScientificForm, paclet:ref/ScientificForm]), (RefLink[EngineeringForm, paclet:ref/EngineeringForm]), and (RefLink[AccountingForm, paclet:ref/AccountingForm]). In fact, you can in principle reproduce the behavior of any one of these functions simply by giving appropriate option settings in one of the others. The default option settings listed in the table are those for (RefLink[NumberForm, paclet:ref/NumberForm]).

Setting (RefLink[DigitBlock, paclet:ref/DigitBlock])->n breaks digits into blocks of length n:

NumberForm[30!,DigitBlock->3]

You can specify any string to use as a separator between blocks of digits:

NumberForm[30!,DigitBlock->5,NumberSeparator->" "]

This gives an explicit plus sign for positive numbers, and uses | in place of a decimal point:

NumberForm[{4.5,-6.8},NumberSigns->{"-","+"},NumberPoint->"|"]

When the Wolfram Language prints an approximate real number, it has to choose whether scientific notation should be used, and if so, how many digits should appear to the left of the decimal point. What the Wolfram Language does is first to find out what the exponent would be if scientific notation were used, and one digit were given to the left of the decimal point. Then it takes this exponent, and applies any function given as the setting for the option (RefLink[ExponentFunction, paclet:ref/ExponentFunction]). This function should return the actual exponent to be used, or (RefLink[Null, paclet:ref/Null]) if scientific notation should not be used.

The default is to use scientific notation for all numbers with exponents outside the range -5 to 5:

{8.^5,11.^7,13.^9}

This uses scientific notation only for numbers with exponents of 10 or more:

NumberForm[%,ExponentFunction->(If[-10<#<10,Null,#]&)]

This forces all exponents to be multiples of 3:

NumberForm[%,ExponentFunction->(3 Quotient[#,3]&)]

Having determined what the mantissa and exponent for a number should be, the final step is to assemble these into the object to print. The option (RefLink[NumberFormat, paclet:ref/NumberFormat]) allows you to give an arbitrary function which specifies the print form for the number. The function takes as arguments three strings: the mantissa, the base, and the exponent for the number. If there is no exponent, it is given as "".

This gives the exponents in Fortran-like "e" format:

NumberForm[{5.6^10,7.8^20},NumberFormat->(SequenceForm[#1,"e",#3]&)]

You can use (RefLink[FortranForm, paclet:ref/FortranForm]) to print individual numbers in Fortran format:

FortranForm[7.8^20]

Controlling the alignment of numbers in output.

Whenever you print a collection of numbers in a column or some other definite arrangement, you typically need to be able to align the numbers in a definite way. Usually you want all the numbers to be set up so that the digit corresponding to a particular power of 10 always appears at the same position within the region used to print a number.

You can change the positions of digits in the printed form of a number by "padding" it in various ways. You can pad on the right, typically adding zeros somewhere after the decimal. Or you can pad on the left, typically inserting spaces in place of leading zeros.

This pads with spaces to make room for up to 7 digits in each integer:

PaddedForm[{456,12345,12},7]

This creates a column of integers:

PaddedForm[Column[{456,12345,12}],7]

This prints each number with room for a total of 7 digits, and with 4 digits to the right of the decimal point:

PaddedForm[{-6.7,6.888,6.99999},{7,4}]

In (RefLink[NumberForm, paclet:ref/NumberForm]), the 7 specifies the maximum precision, but does not make the Wolfram Language pad with spaces:

NumberForm[{-6.7,6.888,6.99999},{7,4}]

If you set the option (RefLink[SignPadding, paclet:ref/SignPadding])->(RefLink[True, paclet:ref/True]), the Wolfram Language will insert leading spaces after the sign:

PaddedForm[{-6.7,6.888,6.99999},{7,4},SignPadding->True]

Only the mantissa portion is aligned when scientific notation is used:

PaddedForm[Column[{6.7 10^8,48.7,-2.3 10^-16}],{4,2}]

With the default setting for the option (RefLink[NumberPadding, paclet:ref/NumberPadding]), both (RefLink[NumberForm, paclet:ref/NumberForm]) and (RefLink[PaddedForm, paclet:ref/PaddedForm]) insert trailing zeros when they pad a number on the right. You can use spaces for padding on both the left and the right by setting (RefLink[NumberPadding, paclet:ref/NumberPadding])->{" "," "}.

This uses spaces instead of zeros for padding on the right:

PaddedForm[{-6.7,6.888,6.99999},{7,4},NumberPadding->{" "," "}]

Printing numbers in other bases.

This prints a number in base 2:

BaseForm[2342424,2]

In bases higher than 10, letters are used for the extra digits:

BaseForm[242345341,16]

(RefLink[BaseForm, paclet:ref/BaseForm]) also works with approximate real numbers:

BaseForm[2.3,2]

You can even use (RefLink[BaseForm, paclet:ref/BaseForm]) for numbers printed in scientific notation:

BaseForm[2.3 10^8,2]

"Digits in Numbers" discusses how to enter numbers in arbitrary bases, and also how to get lists of the digits in a number.