Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2764
| From | Bob Hanlon <hanlonr@cox.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Rounding to a certain number of decimal places |
| Date | 2011-05-28 11:20 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <irqlpq$e9j$1@smc.vnet.net> (permalink) |
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 <adeyoung@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
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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Rounding to a certain number of decimal places Bob Hanlon <hanlonr@cox.net> - 2011-05-28 11:20 +0000
csiph-web