Digits in Numbers

IntegerDigits[n]a list of the decimal digits in the integer n
IntegerDigits[n,b]the digits of n in base b
IntegerDigits[n,b,len]the list of digits padded on the left with zeros to give total length len
IntegerLength[n]the number of decimal digits in n
IntegerLength[n,b]the number of base b digits in n
IntegerExponent[n,b]the number of zeros at the end of n in base b
RealDigits[x]a list of the decimal digits in the approximate real number x, together with the number of digits to the left of the decimal point
RealDigits[x,b]the digits of x in base b
RealDigits[x,b,len]the first len digits of x in base b
RealDigits[x,b,len,n]the first len digits starting with the coefficient of
FromDigits[list]construct a number from its decimal digit sequence
FromDigits[list,b]construct a number from its digit sequence in base b
FromDigits["string"]construct an integer from a string of digits
FromDigits["string",b]construct an integer from a string of digits in base b
IntegerString[n]a string of the decimal digits in the integer n
IntegerString[n,b]a string of the digits of n in base b

Converting between numbers and lists or strings of digits.

Here is the list of base 16 digits for an integer.
In[1]:=
Click for copyable input
Out[1]=
This gives a list of digits, together with the number of digits that appear to the left of the decimal point.
In[2]:=
Click for copyable input
Out[2]=
Here is the binary digit sequence for 56, padded with zeros so that it is of total length 8.
In[3]:=
Click for copyable input
Out[3]=
This reconstructs the original number from its binary digit sequence.
In[4]:=
Click for copyable input
Out[4]=
Here is 56 as a binary string.
In[5]:=
Click for copyable input
Out[5]=
This reconstructs the original number again.
In[6]:=
Click for copyable input
Out[6]=
b^^nnnna number in base b
BaseForm[x,b]print with x in base b
IntegerString[n,b]a string representing n in base b

Numbers in other bases.

When the base is larger than 10, extra digits are represented by letters -.

The number in base 2 is in base 10.
In[7]:=
Click for copyable input
Out[7]=
This prints 37 in base 2.
In[8]:=
Click for copyable input
Out[8]//BaseForm=
This gives the base 2 representation as a string.
In[9]:=
Click for copyable input
Out[9]=
Here is a number in base 16.
In[10]:=
Click for copyable input
Out[10]=
You can do computations with numbers in base 16. Here the result is given in base 10.
In[11]:=
Click for copyable input
Out[11]=
This gives the result in base 16.
In[12]:=
Click for copyable input
Out[12]//BaseForm=
You can give approximate real numbers, as well as integers, in other bases.
In[13]:=
Click for copyable input
Out[13]=
Here are the first few digits of in octal.
In[14]:=
Click for copyable input
Out[14]//BaseForm=
This gives an explicit list of the first 15 octal digits.
In[15]:=
Click for copyable input
Out[15]=
This gives 15 octal digits starting with the coefficient of .
In[16]:=
Click for copyable input
Out[16]=

"Output Formats for Numbers" describes how to print numbers in various formats. If you want to create your own formats, you will often need to use MantissaExponent to separate the pieces of real numbers.

MantissaExponent[x]give a list containing the mantissa and exponent of x
MantissaExponent[x,b]give the mantissa and exponent in base b

Separating the mantissa and exponent of numbers.

This gives a list in which the mantissa and exponent of the number are separated.
In[17]:=
Click for copyable input
Out[17]=
New to Mathematica? Find your learning path »
Have a question? Ask support »