Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Rounding to a certain number of decimal places

1,437 views
Skip to first unread message

Heike Gramberg

unread,
May 28, 2011, 7:18:40 AM5/28/11
to
You can use NumberForm for this. Try something like

NumberForm[a, {Infinity,n}]

This should print a with n digits to the right of the decimal point (adding
zeros when needed).

Heike.

On 27 May 2011, at 11:14, Andrew DeYoung wrote:

> Hi,
>
> I would like to be able to round a number to a certain, specified
> number of decimal places (not number of digits), including zeros.
> Suppose that some function f does this, and takes two arguments, the
> number to be rounded and the number of decimal places to which to
> round: f[number, numPlaces]. Then, for example, I would like the
> following results, if possible:
>
> f[0.8168, 3] = 0.817
> f[1.8168, 3] = 1.817
> f[10.8168, 3] = 10.817
> f[100.8168, 3] = 100.817
> f[0.99, 3] = 0.990
> f[0.9, 3] = 0.900
> f[0.00572, 3] = 0.006
>
> Is there a function f (either built-in or a custom function) that can
> do this? Mathmatica's built-in function NumberForm does not quite do
> this because the user specifies the number of digits, not the number
> of decimal places. Also, NumberForm does not count zeros after the
> decimal point as digits, nor does it add extra zeros as needed if they
> are redundant.
>
> Am I getting into deep water here, or is there something
> straightforward that can do what I would like?
>
> I am running Mathmatica 7. Also, I do not need to be able to do
> further computations on the output; I will be converting the output to
> a string, anyway.
>
> Thank you very much for your time.
>
> Andrew DeYoung
> Carnegie Mellon University
>


Bill Rowe

unread,
May 28, 2011, 7:19:44 AM5/28/11
to
On 5/27/11 at 6:14 AM, adey...@andrew.cmu.edu (Andrew DeYoung)
wrote:

>I would like to be able to round a number to a certain, specified
>number of decimal places (not number of digits), including zeros.
>Suppose that some function f does this, and takes two arguments, the
>number to be rounded and the number of decimal places to which to
>round: f[number, numPlaces]. Then, for example, I would like the
>following results, if possible:

>f[0.8168, 3] = 0.817
>f[1.8168, 3] = 1.817
>f[10.8168, 3] = 10.817
>f[100.8168, 3] = 100.817
>f[0.99, 3] = 0.990
>f[0.9, 3] = 0.900
>f[0.00572, 3] = 0.006

>Is there a function f (either built-in or a custom function) that
>can do this?

Except for .99 and .9 the answer is yes. Round will do what you
want. Specifically,

In[1]:= Round[{0.8168, 1.8168, 10.8168, 100.8168, 0.99, 0.9,
0.00572}, .001]

Out[1]= {0.817,1.817,10.817,100.817,0.99,0.9,0.006}

or if you prefer to specify the number of places past the
decimal point

f[x_, n_] := Round[x, 10^-n]

>Mathmatica's built-in function NumberForm does not
>quite do this because the user specifies the number of digits, not
>the number of decimal places. Also, NumberForm does not count zeros
>after the decimal point as digits, nor does it add extra zeros as
>needed if they are redundant.

Actually, NumberForm can be used. For example,

In[3]:= NumberForm[.9, {1, 3}]

Out[3]//NumberForm= 0.900

or

In[4]:= NumberForm[10.8168, {5, 3}]

Out[4]//NumberForm= 10.817

In fact for the case of .9 and .99 you have to use something
like NumberForm since Mathematica will not display trailing
zeros for any function that returns a number.

Note, there is a key difference between Round[x,.001] and
NumberForm[x,{n,3}]. Round outputs a value that is different
from the starting value that can be used in subsequent
computation. NumberForm controls the display of the value by
putting a wrapper around the expression being evaluated. The
point is you can do something like

In[5]:= Rationalize@Round[10.8168, .001]

Out[5]= 10817/1000

which demonstrates Round actually changes the value. But you
cannot do something similar with NumberForm.


Bob Hanlon

unread,
May 28, 2011, 7:20:26 AM5/28/11
to

val = {0, 0.8168, 1.8168, 10.8168, 100.8168, 0.99, 0.9, 0.006};

myRound[x_, n_] := NumberForm[Round[x, 10.^-n],
{Max[Floor[Log10[x] + n + 1], 1], n}]

Round[val, 10.^-3]

{0., 0.817, 1.817, 10.817, 100.817, 0.99, 0.9, 0.006}

myRound[#, 3] & /@ val

{0.000, 0.817, 1.817, 10.817, 100.817, 0.990, 0.900, 0.006}


Bob Hanlon

---- Andrew DeYoung <adey...@andrew.cmu.edu> wrote:

=============
Hi,

I would like to be able to round a number to a certain, specified
number of decimal places (not number of digits), including zeros.
Suppose that some function f does this, and takes two arguments, the
number to be rounded and the number of decimal places to which to
round: f[number, numPlaces]. Then, for example, I would like the
following results, if possible:

f[0.8168, 3] = 0.817
f[1.8168, 3] = 1.817
f[10.8168, 3] = 10.817
f[100.8168, 3] = 100.817
f[0.99, 3] = 0.990
f[0.9, 3] = 0.900
f[0.00572, 3] = 0.006

Is there a function f (either built-in or a custom function) that can

do this? Mathmatica's built-in function NumberForm does not quite do


this because the user specifies the number of digits, not the number
of decimal places. Also, NumberForm does not count zeros after the
decimal point as digits, nor does it add extra zeros as needed if they
are redundant.

Am I getting into deep water here, or is there something

Murray Eisenberg

unread,
May 28, 2011, 7:21:30 AM5/28/11
to
You seem to be confusing two meanings of "round", or at least confusing
rounding with specifying number of digits shown. For example,

Round[0.99,10.^-3]
0.99

but

NumberForm[0.99,{2,3}]
0.990

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Daniel Lichtblau

unread,
May 28, 2011, 7:21:40 AM5/28/11
to

To obtain numbers of the desired form, either of f or f2 will suffice.
The key issue is to force the Accuracy (in Mathematica's usage) to be
the desired number of digits. Some experimenting showed me I needed to
add a hair beyond the integer specification in order to have them come
out right.

f[n_, d_Integer] := N[Rationalize[n, 0], {Infinity, d + 10^(-2)}]

f2[n_, d_Integer] := SetAccuracy[n, d + 10^(-2)]

If all that is of interest is the formatting, can use NumberForm as below.

f3[n_, d_Integer] := NumberForm[n, {Infinity, d}]

Daniel Lichtblau
Wolfram Research

0 new messages