Output Formats for Numbers
| ScientificForm[expr] | print all numbers in scientific notation |
| EngineeringForm[expr] | print all numbers in engineering notation (exponents divisible by 3) |
| AccountingForm[expr] | print all numbers in standard accounting format |
Output formats for numbers.
These numbers are given in the default output format. Large numbers are given in scientific notation.
| Out[1]= |  |
|
This gives all numbers in scientific notation.
Out[2]//ScientificForm= |
| |  |
|
This gives the numbers in engineering notation, with exponents arranged to be multiples of three.
Out[3]//EngineeringForm= |
| |  |
|
In accounting form, negative numbers are given in parentheses, and scientific notation is never used.
Out[4]//AccountingForm= |
| |  |
|
| NumberForm[expr,tot] | print at most tot digits of all approximate real numbers in expr |
| ScientificForm[expr,tot] | use scientific notation with at most tot digits |
| EngineeringForm[expr,tot] | use engineering notation with at most tot digits |
Controlling the printed precision of real numbers.
Here is 9 to 30 decimal places.
| Out[5]= |  |
|
This prints just 10 digits of 9.
Out[6]//NumberForm= |
| |  |
|
This gives 12 digits, in engineering notation.
Out[7]//EngineeringForm= |
| |  |
|
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
NumberForm,
ScientificForm,
EngineeringForm and
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
NumberForm.
Setting DigitBlock->n breaks digits into blocks of length n.
Out[8]//NumberForm= |
| |  |
|
You can specify any string to use as a separator between blocks of digits.
Out[9]//NumberForm= |
| |  |
|
This gives an explicit plus sign for positive numbers, and uses | in place of a decimal point.
Out[10]//NumberForm= |
| |  |
|
When
Mathematica 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
Mathematica 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
ExponentFunction. This function should return the actual exponent to be used, or
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.
| Out[11]= |  |
|
This uses scientific notation only for numbers with exponents of 10 or more.
Out[12]//NumberForm= |
| |  |
|
This forces all exponents to be multiples of 3.
Out[13]//NumberForm= |
| |  |
|
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
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.
Out[14]//NumberForm= |
| |  |
|
You can use FortranForm to print individual numbers in Fortran format.
Out[15]//FortranForm= |
| |  |
|
| PaddedForm[expr,tot] | print with all numbers having room for tot digits, padding with leading spaces if necessary |
| PaddedForm[expr,{tot,frac}] | print with all numbers having room for tot digits, with exactly frac digits to the right of the decimal point |
| NumberForm[expr,{tot,frac}] | print with all numbers having at most tot digits, exactly frac of them to the right of the decimal point |
| Column[{expr1,expr2,...}] | print with the expri left aligned in a column |
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.
Out[16]//PaddedForm= |
| |  |
|
This creates a column of integers.
Out[17]//PaddedForm= |
| |  |
|
This prints each number with room for a total of 7 digits, and with 4 digits to the right of the decimal point.
Out[18]//PaddedForm= |
| |  |
|
In NumberForm, the 7 specifies the maximum precision, but does not make Mathematica pad with spaces.
Out[19]//NumberForm= |
| |  |
|
If you set the option SignPadding->True, Mathematica will insert leading spaces after the sign.
Out[20]//PaddedForm= |
| |  |
|
Only the mantissa portion is aligned when scientific notation is used.
Out[21]//PaddedForm= |
| |  |
|
With the default setting for the option
NumberPadding, both
NumberForm and
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
NumberPadding->{" ", " "}.
This uses spaces instead of zeros for padding on the right.
Out[22]//PaddedForm= |
| |  |
|
| BaseForm[expr,b] | print with all numbers given in base b |
Printing numbers in other bases.
This prints a number in base 2.
Out[23]//BaseForm= |
| |  |
|
In bases higher than 10, letters are used for the extra digits.
Out[24]//BaseForm= |
| |  |
|
BaseForm also works with approximate real numbers.
Out[25]//BaseForm= |
| |  |
|
You can even use BaseForm for numbers printed in scientific notation.
Out[26]//BaseForm= |
| |  |
|
"Digits in Numbers" discusses how to enter numbers in arbitrary bases, and also how to get lists of the digits in a number.