Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2747 > unrolled thread
| Started by | Andrew DeYoung <adeyoung@andrew.cmu.edu> |
|---|---|
| First post | 2011-05-27 10:14 +0000 |
| Last post | 2011-05-28 11:21 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Rounding to a certain number of decimal places Andrew DeYoung <adeyoung@andrew.cmu.edu> - 2011-05-27 10:14 +0000
Re: Rounding to a certain number of decimal places Albert Retey <awnl@gmx-topmail.de> - 2011-05-28 11:19 +0000
Re: Rounding to a certain number of decimal places Peter Breitfeld <phbrf@t-online.de> - 2011-05-28 11:20 +0000
Re: Rounding to a certain number of decimal places Ray Koopman <koopman@sfu.ca> - 2011-05-28 11:20 +0000
Re: Rounding to a certain number of decimal places "Kevin J. McCann" <kjm@KevinMcCann.com> - 2011-05-28 11:21 +0000
| From | Andrew DeYoung <adeyoung@andrew.cmu.edu> |
|---|---|
| Date | 2011-05-27 10:14 +0000 |
| Subject | Rounding to a certain number of decimal places |
| Message-ID | <irnthn$sq0$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-05-28 11:19 +0000 |
| Message-ID | <irqlnr$e7q$1@smc.vnet.net> |
| In reply to | #2747 |
Am 27.05.2011 12:14, schrieb Andrew DeYoung:
> 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
>
you could try:
f[x_]:=NumberForm[x, {10, 3}]
hth,
albert
[toc] | [prev] | [next] | [standalone]
| From | Peter Breitfeld <phbrf@t-online.de> |
|---|---|
| Date | 2011-05-28 11:20 +0000 |
| Message-ID | <irqlq5$e9u$1@smc.vnet.net> |
| In reply to | #2747 |
This seems to work:
rounded[x_?NumericQ, places_Integer] :=
With[{iP = Length[IntegerDigits[IntegerPart[x]]]},
PaddedForm[N[Round[x 10^places]/10^places], {iP + places, places}]]
rounded[x_, places_] := x
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
>
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-05-28 11:20 +0000 |
| Message-ID | <irqlqq$eaf$1@smc.vnet.net> |
| In reply to | #2747 |
On May 27, 3:14 am, Andrew DeYoung <adeyo...@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
f[x_,n_] := PaddedForm[x,{n+1+Floor@Log[10,Max[1,Abs@x]],n}]
[toc] | [prev] | [next] | [standalone]
| From | "Kevin J. McCann" <kjm@KevinMcCann.com> |
|---|---|
| Date | 2011-05-28 11:21 +0000 |
| Message-ID | <irqlrf$eb4$1@smc.vnet.net> |
| In reply to | #2747 |
Try NumberForm On 5/27/2011 6:14 AM, 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 >
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web