# Real Number to a string conversion so I can format my output in a Grid

Was looking for something like Matlab's num2str function. I'm trying to use Grid to format output from a print statement, but some of the cells are complex like

"distance result = ", list[], " meters "


The Print function has no problem with that

Print["distance result = ", list[], " meters "]
Print["time result = ", list[], " days "]


but if I try

Grid[{{"distance result = " <> IntegerString[list[]]}," meters "},{"time result = " <> IntegerString[list[]]," days "}},Alignment-> Left]


This only works if list[] and list[] are integers. How do I do this if list contains real numbers, i.e., IntegerString no longer works? I would like meters and days to be vertically aligned. I think I need to intermix text and real numbers inside a single grid cell. Maybe there is a better way to format outputs? Do I put separate Print statements inside each Grid cell? Not sure how to get properly formatted multi-line output in notebooks.

• ToString should do it – Stitch Apr 18 '17 at 19:18

You need to use ToString:

list = {1.5, 2.5, 3.5};
Grid[{{"distance result = " <> ToString[list[]], " meters "},
{"time result = " <> ToString[list[]], " days "}}, Alignment -> Left]

• You might want to look at Alignment -> "=" – Bob Hanlon Apr 18 '17 at 19:25

I would use Row instead of converting everything to a string and concatenating. Also, you can use the built-in Quantity system as well. So, something like:

list = {1, 2.2, 3.333};
Column[{
Row[{"distance result = ", Quantity[list[], "Meters"]}],
Row[{"time result = ", Quantity[list[], "Days"]}]
},
Alignment->"="
] There are several issues when converting to a string. In your case, a problematic example is a number like 1.1*10^-6:

ToString[1.1*10^-6] //InputForm

(* "      -6\n1.1 10" *)

• This is nice too. Quantity looks like a neat command. The example I gave was a bit simplified as my list vector might not be the only item I want to put in a cell. There might be more text and more numbers all needing to go into one cell. But thanks for steering me to Quantity and Row. I do like how Row helps visually organize the cells. – Tom Mozdzen Apr 18 '17 at 20:42