# First 10 digits of $\pi$ without rounding

I need to have $\pi$ with an accuracy of $10$ digits. My first guess would be

N[Pi, 10]


However, this rounds to the last digit instead of just chopping of any additional digits and hence the last digit is $4$ (and not $3$, as it actually is).

I could do

N[FromDigits[RealDigits[Pi, 10, 10]], 10]


but I am pretty sure that there are nicer solutions.

• Maybe IntegerPart[Pi 10^9]/10.^9:) – Kuba Dec 16 '13 at 21:24
• Floor[N[Pi, 11], 0.000000001]. Frankly, this looks like a non-problem. – Sjoerd C. de Vries Dec 16 '13 at 21:27
• If you just want to print it..StringTake[ToString[N[Pi, 11]], 11] – george2079 Dec 16 '13 at 21:34
• 3.141592653 :-) – Cassini Dec 16 '13 at 22:51
• Thanks for those suggestions. Actually, Pi is just an example for a number. So what I want is to get the first n digits of a number a, without rounding. – traindriver Dec 17 '13 at 10:57

If you only need 10 digits of precision there's a ComputerArithmetic package that allows you to specify your rounding method under limited conditions.

Needs["ComputerArithmetic"]
SetArithmetic[10, RoundingRule->Truncation]


This will set up our arithmetic system, but in order to use it, we'll need to use ComputerNumber[]

N[ComputerNumber[Pi],10]


yields an output of 3.141592653

N[ComputerNumber[EulerGamma],10]


yields an output of 0.5772156649. But keep in mind this is 10 total digits of precision so if we do this:

N[ComputerNumber[100*EulerGamma],10]


we'll get 57.72156649 and not 57.7215664901

If you require more flexible precision you'll want to do what Sjoerd C. de Vries recommended. I've wrapped everything in a SetPrecision[] for clarity because without it Mathematica will hide the output of everything but the first 6 significant digits:

NTrunc[number_, precision_Integer] :=
SetPrecision[
Floor[
N[number, precision + 1],
10^-(precision - 1)],
precision]


Here is a round-about method, after the BBP algorithm (discovered by a computer! * ) :-

Round[NSum[
(4/(8 n + 1) - 2/(8 n + 4) - 1/(8 n + 5) - 1/(8 n + 6))/16^n,
{n, 0, 5}], 10.^-9] // InputForm


3.141592653

* "The formula itself was found by a computer program, and almost certainly constitutes the first instance of a computer program finding a significant new formula for pi." - D.H.Bailey

• hmm.. i think this only works because the approximation happens to round the right way. Were there a base 10 anaolg of the bpp formula then you'd have something. – george2079 Dec 16 '13 at 23:38
• @george2079 - yes, the approximate rounding was convenient ;-) – Chris Degnen Dec 16 '13 at 23:46
• I hope it was obvious that I only summed to the 5th term rather than infinity, as the proper formula has it. – Chris Degnen Dec 16 '13 at 23:54
• interesting! but as I just commented above, I am not only interested in Pi but also other numbers. – traindriver Dec 17 '13 at 11:00
• @traindriver - then I would have said your RealDigits method was best, but hifigi's ComputerNumber is even better, and it works with negative numbers. – Chris Degnen Dec 18 '13 at 9:11

I liked george2079 solution in the comments above, so here is a Manipulate using it, just for fun (I have slow coffee maker :) Manipulate[
n,
Grid[{
{N[Pi, 50], SpanFromLeft},
{Dynamic[StringTake[ToString[N[Pi, n]], n]], SpanFromLeft},
{Control[{{n, 10, "N?"}, 2, 50, 1, ImageSize -> Medium}],
Dynamic[n]}
}, Alignment -> Left],
TrackedSymbols :> {n},
ImageMargins -> 0,
FrameMargins -> 0,
ContentSize -> {0},
`