3
$\begingroup$

This question is very closely related to two earlier ones:

Rounding to the nearest decimal

and

How do you round numbers so that it affects computation?

but confronts an additional issue not addressed by those answers. (Incidentally, the only way you can appreciate my question is by running code on your machine... merely inspecting the below does not suffice.)

I want to generate a fixed list of random numbers (from a distribution, but the below illustrates the problem), but for space and readability, I want to keep only three digits to the right of the decimal point. This code

Round[#, .001] & /@ RandomReal[{1, 10}, 3]

gives the following output (which I typed in by hand):

(* {9.41, 7.26, 4.298} *)

This looks fine. However, if you simply copy everything from that output cell and it paste into an input cell, you get "`" markers after each number, and in some cases many more digits than the three desired.

I've tried all manner of Round, Ceiling and Floor, such as:

N@(Round[1000 #] & /@ RandomReal[{1, 10}, 3])/1000

and

myRound[x_, n_] := Ceiling[10^n x]/10^n // N;
myRound[#, 3] & /@ RandomReal[{1, 10}, 3]

and can get appropriate outputs, which show three digits, as desired. Nevertheless, in every case when I cut such output and paste it into an input cell, the "`" markers or extraneous digits appear.

How do I get lists of "true" fixed-digit numbers for input cells?

$\endgroup$
8
  • $\begingroup$ Would conversion to text with ToString be acceptable? $\endgroup$
    – Yves Klett
    Commented Jan 8, 2015 at 20:40
  • $\begingroup$ @YvesKlett: I tried ToString but it is so awkward and kludgy. So no. Isn't there a better way? $\endgroup$ Commented Jan 8, 2015 at 21:17
  • $\begingroup$ @DavidG.Stork I'd do NumberForm[RandomReal[], {Infinity, 3}]. $\endgroup$
    – Kuba
    Commented Jan 8, 2015 at 21:22
  • 1
    $\begingroup$ Quite probably. Could you elaborate on why / in which context you want to copy/paste manually? $\endgroup$
    – Yves Klett
    Commented Jan 8, 2015 at 21:27
  • 2
    $\begingroup$ Right click and "copy as plain text"..? $\endgroup$
    – george2079
    Commented Jan 8, 2015 at 21:35

3 Answers 3

5
$\begingroup$

EDIT

Actually, the previous code I gave in 1/ to round numbers is not exactly rounding the numbers ... , it is just removing all the digits that are not "needed". For example myround[1.3458,3]returns 1.345 instead of 1.346 !

Anyway, I just found out a much simpler solution :

0/

  • Use for example Round[number, 10^-3] instead of Round[number, 0.001] to prevent from getting the "extraneous digits" that appear when you copy/paste.

  • As before, use InputForm in order to suppress the NumberMarks when you copy/paste

For example, concerning the extraneous digits, compare :

InputForm@N[Round[#, 10^-3] & /@ RandomReal[{1, 10}, 100]] 

with

InputForm@N[Round[#, 0.001] & /@ RandomReal[{1, 10}, 100]]

=======================================================================

PREVIOUS

Is this working for you ?

1/

This code will produce exactly a real with at most n digits to the right of the decimal point :

myround[x_, n_] := IntegerPart[x] + IntegerPart[10^n*FractionalPart[x]]/10^n

then

InputForm[N@(myround[#, 3] & /@ RandomReal[{1, 10}, 10])]

where InputForm makes it possible to copy/paste the output without the NumberMarks following each real.

It seems there is no problem also to copy/paste the output of :

InputForm[
 N@(myround[#, 3] & /@ 
    RandomVariate[
     MultinormalDistribution[{-1.5, 0}, {{2, 0}, {0, 1}}], {10}])]

2/

Whenever you need to remove NumberMarks you can run for example :

InputForm[{8.953`, 4.801`, 8.098`, 5.558`, 1.856`, 8.602`, 9.468`, 4.458`,
 4.538`, 3.348`}, NumberMarks->False]

{8.953, 4.801, 8.098, 5.558, 1.856, 8.602, 9.468, 4.458, 4.538, 3.348}

which can be copied then pasted without the NumberMarks.

$\endgroup$
4
  • $\begingroup$ Yes... /1 works. Thanks so much. (2/ is rather ugly, but might be useful as a backup.) Seems like a lot of work for such a simple functionality, though. $\endgroup$ Commented Jan 9, 2015 at 0:47
  • $\begingroup$ @DavidG.Stork Please see my edit. myround is not really rounding numbers which may not be problematic for your problem but it is not correct. Added simpler solution. $\endgroup$
    – SquareOne
    Commented Jan 9, 2015 at 1:46
  • $\begingroup$ your myround doesn't round properly either.. N@myround[ 0.1299, 2] -> 0.12 .. not really relevant to the question though. $\endgroup$
    – george2079
    Commented Jan 9, 2015 at 14:08
  • $\begingroup$ @george2079 Did you read my last edit ?? The myroundis actually what is called rounding down. It is indeed relevant to the question because the OP just needs to record some random values with fixed number of digits. However, for the usual rounding see the new answer I gave. $\endgroup$
    – SquareOne
    Commented Jan 9, 2015 at 18:16
3
$\begingroup$
Composition[
  CellPrint,
  Cell[#, "Input"] &,
  BoxData,
  RowBox,
  Riffle[#, ","] &,
  ToString /@ # &,
  NumberForm[#, {Infinity, 3}] & /@ # &
  ]@RandomReal[1, 10]

Gives

0.179, 0.915, 0.499, 0.476, 0.179, 0.331, 0.233, 0.414, 0.520, 0.331

which is already an input cell but you can copy it wherever you need.

$\endgroup$
3
  • 1
    $\begingroup$ This works fine. Thanks. But it is surprising that such code is needed for such a simple function. $\endgroup$ Commented Jan 8, 2015 at 22:34
  • 1
    $\begingroup$ @DavidG.Stork I agree, general and precise numbers formatting in mma always makes me insecure about my skills :(. p.s. you can also upvote a question/answer you find useful (gray triangle above the score). $\endgroup$
    – Kuba
    Commented Jan 8, 2015 at 22:42
  • $\begingroup$ I accepted your answer too quickly. It turns out that it actually doesn't work properly: Take two outputs from your code, wrap { } around each to form two lists, a and b, and then Transpose[a,b]. Frustrating: the "`"s are re-introduced after all that! It seems as though any cutting and pasting to a full input cell cannot work. Goal: true three-decimal two-dimensional points from RandomVariate[ MultinormalDistribution[{-1.5, 0}, {{2, 0}, {0, 1}}], {10}]. See if you can do that! Your code won't do it (as far as I see). $\endgroup$ Commented Jan 8, 2015 at 23:33
0
$\begingroup$

An alternative to using SquareOne's suggestion to use InputForm, you can also modify the stylesheet so that number marks are suppressed in "Input" cells. For example:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Input"],NumberMarks->False]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

The issue with removing NumberMarks is that you can no longer look at a number and determine whether it is a machine number or an extended precision number, since they will now look the same. For example, copy/paste the output from the following input:

{.123, N[123/1000,3]}

{0.123, 0.123}

They will look the same:

{0.123, 0.123}

If you do the copy/paste with the default stylesheet you get:

{0.123`, 0.123`3.}

and you can tell that the first number is a machine number, while the second number is an extended precision number with 3 digits of precision.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.