({{, (RefLink[RealDigits, paclet:ref/RealDigits])[x]
gives a list of the digits in the approximate real number x, together with the number of digits that are to the left of the decimal point. }, {, (RefLink[RealDigits, paclet:ref/RealDigits])[x,b]
gives a list of base-b digits in x. }, {, (RefLink[RealDigits, paclet:ref/RealDigits])[x,b,len]
gives a list of len digits. }, {, (RefLink[RealDigits, paclet:ref/RealDigits])[x,b,len,n]
gives len digits starting with the coefficient of (b^n). }})

OpenersNotesSection(Spacer1)Details

(RefLink[RealDigits, paclet:ref/RealDigits]) gives the most significant digits first, as in standard positional notation.

(RefLink[RealDigits, paclet:ref/RealDigits])[x] normally returns a list of digits of length (RefLink[Round, paclet:ref/Round])[(RefLink[Precision, paclet:ref/Precision])[x]].

(RefLink[RealDigits, paclet:ref/RealDigits])[x] and (RefLink[RealDigits, paclet:ref/RealDigits])[x,b] normally require that x be an approximate real number, returned for example by (RefLink[N, paclet:ref/N]). (RefLink[RealDigits, paclet:ref/RealDigits])[x,b,len] also works on exact numbers.

For integers and rational numbers with terminating digit expansions, (RefLink[RealDigits, paclet:ref/RealDigits])[x] returns an ordinary list of digits. For rational numbers with non-terminating digit expansions, it yields a list of the form {(Subscript[a, 1]),(Subscript[a, 2]),…,{(Subscript[b, 1]),(Subscript[b, 2]),…}} representing the digit sequence consisting of the (Subscript[a, i]) followed by infinite cyclic repetitions of the (Subscript[b, i]). »

If len is larger than (RefLink[Precision, paclet:ref/Precision])[x]/(RefLink[Log, paclet:ref/Log])[10,b], then remaining digits are filled in as (RefLink[Indeterminate, paclet:ref/Indeterminate]).

(RefLink[RealDigits, paclet:ref/RealDigits])[x,b,len,n] starts with the digit which is the coefficient of (b^n), truncating or padding with zeros as necessary. »

(RefLink[RealDigits, paclet:ref/RealDigits])[x,b,len,-1] starts with the digit immediately to the right of the base-b decimal point in x.

(RefLink[RealDigits, paclet:ref/RealDigits])[x,b,(RefLink[Automatic, paclet:ref/Automatic]),n] gives as many digits as it can in a fixed-precision number.

The base b in (RefLink[RealDigits, paclet:ref/RealDigits])[x,b] need not be an integer. For any real b such that b>1, (RefLink[RealDigits, paclet:ref/RealDigits])[x,b] successively finds the largest integer multiples of powers of b that can be removed while leaving a non-negative remainder.

(RefLink[RealDigits, paclet:ref/RealDigits])[x] discards the sign of x.

(RefLink[RealDigits, paclet:ref/RealDigits])[0.] gives {{0},-(RefLink[Floor, paclet:ref/Floor])[(RefLink[Accuracy, paclet:ref/Accuracy])[0.]]}.

(RefLink[FromDigits, paclet:ref/FromDigits]) can be used as the inverse of (RefLink[RealDigits, paclet:ref/RealDigits]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (18)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Give the list of digits and exponent:

RealDigits[123.55555]

RealDigits[0.000012355555]

Give 25 digits of π in base 10:

RealDigits[Pi,10,25]

Give 25 digits of 19/7:

RealDigits[19/7,10,25]

Give an explicit recurring decimal form:

RealDigits[19/7]

OpenersExampleSection1(Spacer1)Scope  (3)

Base-2 digits:

RealDigits[1.234,2,15]

RealDigits[Pi,10,20]

20 digits starting with the coefficient of (10^(-5)):

RealDigits[Pi,10,20,-5]

20 digits starting with the coefficient of (10^5):

RealDigits[Pi,10,20,5]

Noninteger bases are allowed:

RealDigits[Pi,GoldenRatio,20]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (2)

(RefLink[RealDigits, paclet:ref/RealDigits]) gives (RefLink[Indeterminate, paclet:ref/Indeterminate]) if more digits than the precision are requested:

RealDigits[5.635,10,20]

Start at the coefficient of (10^(-15)):

RealDigits[5.635,10,20,-15]

Include only digits that are determined by the precision available:

RealDigits[5.635,10,Automatic,-15]

OpenersExampleSection3(Spacer1)Applications  (6)

The 10000(^th) digit of π is an 8:

RealDigits[Pi,10,1,-10000]

Number of 1s in the first million base-2 digits of π:

Count[First[RealDigits[Pi,2,10^6]],1]

Distribution of first 100000 digits of π in base 47:

ListPlot[BinCounts[First[RealDigits[Pi,47,100000]],{0,46}],DataRange->{0,46}]

Fibonacci representations of integers:

Table[RealDigits[n,GoldenRatio,10],{n,5}]

Binary representation of a machine number:

r = RandomReal[]

{d,e}=RealDigits[r,2]

(RefLink[MachinePrecision, paclet:ref/MachinePrecision]) is equal to the number of bits times (Subscript[log, 10])(2):

MachinePrecision==Length[d]*Log[10.,2]

Get the next larger machine number:

d[[-1]]+=1;
rp=N[FromDigits[{d,e},2]]

The spacing between these numbers is (2^((e-1))) (RefLink[$MachineEpsilon, paclet:ref/$MachineEpsilon]):

rp -r == 2^(e-1)*$MachineEpsilon

Find the error in representing 1/10 with a machine number:

RealDigits[.1,2]

err = FromDigits[%,2]-1/10

N[%]

The next smaller machine number is farther away:

{d,e}=RealDigits[.1,2];
d[[-1]]-=1;
N[FromDigits[{d,e},2]]-1/10

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

The default number of digits returned is determined by the precision of the number:

p=RandomReal[{20,100}];

{d,e}=RealDigits[N[Pi,p]]

Length[d] == Round[p]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

Digits unknown at the available precision are filled in as (RefLink[Indeterminate, paclet:ref/Indeterminate]):

RealDigits[1.234,10,20]

For non-binary bases, the digits given may not be enough to reconstruct the number exactly:

x = 1 +$MachineEpsilon;
{d,e}=RealDigits[1+$MachineEpsilon]

More than (RefLink[Round, paclet:ref/Round])[(RefLink[MachinePrecision, paclet:ref/MachinePrecision])] decimal digits are required to separate x from 1:

FromDigits[{d,e}]

(RefLink[InputForm, paclet:ref/InputForm]) uses a sufficient number of digits to uniquely reconstruct the number:

InputForm[x]

x-1.0000000000000002

OpenersExampleSection6(Spacer1)Neat Examples  (1)

A hundred digits of π starting with the millionth digit:

Row[First@RealDigits[Pi,10,100,-10^6]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[MantissaExponent, paclet:ref/MantissaExponent])  ▪  (RefLink[IntegerDigits, paclet:ref/IntegerDigits])  ▪  (RefLink[BaseForm, paclet:ref/BaseForm])  ▪  (RefLink[FromDigits, paclet:ref/FromDigits])  ▪  (RefLink[ContinuedFraction, paclet:ref/ContinuedFraction])  ▪  (RefLink[MultiplicativeOrder, paclet:ref/MultiplicativeOrder])  ▪  (RefLink[ChampernowneNumber, paclet:ref/ChampernowneNumber])  ▪  (RefLink[FindTransientRepeat, paclet:ref/FindTransientRepeat])  ▪  (RefLink[DecimalForm, paclet:ref/DecimalForm])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Digits in Numbers, paclet:tutorial/DigitsInNumbers])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Constructing Lists, paclet:guide/ConstructingLists])}, {▪(RefLinkPlain[Number Digits, paclet:guide/NumberDigits])}, {▪(RefLinkPlain[Representation of Numbers, paclet:guide/RepresentationOfNumbers])}, {▪(RefLinkPlain[Numerical Evaluation & Precision, paclet:guide/NumericalEvaluationAndPrecision])}, {▪(RefLinkPlain[Number Theory, paclet:guide/NumberTheory])}, {▪(RefLinkPlain[Atomic Elements of Expressions, paclet:guide/AtomicElementsOfExpressions])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(Spacer1)▪(RefLinkPlain[(Spacer1)Implementation notes: Numerical and Related Functions, paclet:tutorial/SomeNotesOnInternalImplementation#23292])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/23-more-about-numbers.html])(WebLink[: More about Numbers, https://www.wolfram.com/language/elementary-introduction/23-more-about-numbers.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=RealDigits]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})