**3.1.3 Converting between Different Forms of Numbers**

Functions that convert between different types of numbers.

This gives a 30-digit real number approximation to 3/7.
In[1]:= **N[3/7, 30]**

Out[1]=

This takes the 30-digit number you have just generated, and reduces it to 20-digit precision.
In[2]:= **N[%, 20]**

Out[2]=

This converts the result back to a rational number.
In[3]:= **Rationalize[%]**

Out[3]=

The numerical value of

is not "sufficiently close" to a rational number to be converted.
In[4]:= **Rationalize[ N[Pi] ]**

Out[4]=

If you give a specific tolerance, Rationalize will give you a rational number approximation accurate to within that tolerance.
In[5]:= **Rationalize[ N[Pi], 10^-5 ]**

Out[5]=

With a tolerance of 0, Rationalize yields the best possible rational approximation given the precision of your input.
In[6]:= **Rationalize[ N[Pi], 0 ]**

Out[6]=

Converting between numbers and lists of digits.

Here is the list of base 16 digits for an integer.
In[7]:= **IntegerDigits[1234135634, 16]**

Out[7]=

This gives a list of digits, together with the number of digits that appear to the left of the decimal point.
In[8]:= **RealDigits[123.456789012345678901234567]**

Out[8]=

Here is the binary digit sequence for 56, padded with zeros so that it is of total length 8.
In[9]:= **IntegerDigits[56, 2, 8]**

Out[9]=

This reconstructs the original number from its binary digit sequence.
In[10]:= **FromDigits[%, 2]**

Out[10]=

Numbers in other bases.

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

The number in base 2 is

in base 10.
In[11]:= **2^^100101**

Out[11]=

This prints

in base 2.
In[12]:= **BaseForm[37, 2]**

Out[12]//BaseForm=

Here is a number in base 16.
In[13]:= **16^^ffffaa00**

Out[13]=

You can do computations with numbers in base 16. Here the result is given in base 10.
In[14]:= **16^^fffaa2 + 16^^ff - 1**

Out[14]=

This gives the result in base 16.
In[15]:= **BaseForm[%, 16]**

Out[15]//BaseForm=

You can give approximate real numbers, as well as integers, in other bases.
In[16]:= **2^^101.100101**

Out[16]=

Here are the first few digits of

in octal.
In[17]:= **BaseForm[N[Sqrt[2], 30], 8]**

Out[17]//BaseForm=

This gives an explicit list of the first 15 octal digits.
In[18]:= **RealDigits[Sqrt[2], 8, 15]**

Out[18]=

This gives 15 octal digits starting with the coefficient of

.
In[19]:= **RealDigits[Sqrt[2], 8, 15, -10]**

Out[19]=

Section 2.8.7 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 approximate real numbers.

Separating the mantissa and exponent of numbers.

This gives a list in which the mantissa and exponent of the number are separated.
In[20]:= **MantissaExponent[3.45 10^125]**

Out[20]=