# Rounding to the nearest decimal and pasting such output into an Input cell

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?

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

## 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.

• 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. – David G. Stork Jan 9 '15 at 0:47
• @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. – SquareOne Jan 9 '15 at 1:46
• your myround doesn't round properly either.. N@myround[ 0.1299, 2] -> 0.12 .. not really relevant to the question though. – george2079 Jan 9 '15 at 14:08
• @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. – SquareOne Jan 9 '15 at 18:16
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.

• This works fine. Thanks. But it is surprising that such code is needed for such a simple function. – David G. Stork Jan 8 '15 at 22:34
• @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). – Kuba Jan 8 '15 at 22:42
• 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). – David G. Stork Jan 8 '15 at 23:33