# Conditionally formatting elements in a list

I have a list {{a1, a2, a3},{b1, b2, b3}, ...} and I want to replace the numbers less than 10 by 0x (just insert a 0 before the element, so that 2 -> 02, 3 -> 03, etc). How can I do that?

To clarify, I will later export this table and run it through a program that will upload it to a database. These number are identifications for different objects, and for some reason the list needs to be all aligned. If I just export it as it is, I would get something like

1  2  3
20  40  55


which the program can't read.

-
so you want to convert numbers to strings ... –  belisarius Jun 4 '14 at 18:07
What are the elements of your list? What is your motivation for wanting to do this? 02 is precisely the same number as 2. Are you working with numbers or strings? It sounds like you want IntegerString[..., 10, 2]. –  Szabolcs Jun 4 '14 at 18:09
Presumably, it is to align elements in a Table or Grid, e.g., list = RandomInteger[{1, 20}, {10, 3}]; IntegerString[list, 10, 2] // Grid –  Bob Hanlon Jun 4 '14 at 19:12
Yes, sorry if it wasn't clear, but I need to export the file and it would be easier to have it all aligned in mathematica than to add the zeroes later myself. –  bernie Jun 10 '14 at 18:04

## 4 Answers

This may be a job for IntegerString:

m = RandomInteger[15, {2, 5}]

{{4, 8, 3, 14, 0}, {12, 7, 11, 1, 7}}

IntegerString[m, 10, 2]

{{"04", "08", "03", "14", "00"}, {"12", "07", "11", "01", "07"}}


By default string characters are not printed in tables and grids so it looks like this:

% // TableForm


-

I am guessing that you need NumberForm with padding with 0 on the left:

lst = RandomInteger[20, {10, 5}];
newlst = Map[If[# >= 10, #,
NumberForm[#, 1, NumberPadding -> {"0", ""}]] &, lst, {-1}];

Row[Panel /@ (Style[#, 20] & /@ TableForm /@ {lst, newlst}), Spacer[5]]


Note:

NumberForm acts as a "wrapper", which affects printing, but not evaluation.

-
thanks. It doesn't however address negative number, and they stay as -2 insted of -02 –  bernie Jun 10 '14 at 18:14

This may be easier.

lst = RandomInteger[20, {10, 5}];
lst /. x_?(# < 10 &) -> NumberForm[x, 1, NumberPadding -> {"0", ""}]


-
I have a similar problem but with dates. I need to change the format of an entry if it is a string representing a date (18/12/2013 19:53 for example) and leave it alone if it is not. I think I need to Map my date function to the list, but conditionally so that it doesn't map on to strings that are not dates. How can I do this? –  Lara Jordan Oct 15 '14 at 5:58
is this good for you: {"18/12/2013 19:53", "r", 5} /. _String?(StringMatchQ[#, ___ ~~ "/" ~~ ___] &) :> ee –  Algohi Oct 15 '14 at 19:52
I will try that but can you please explain what you have written in words so I know what it means? I think it means "take this list {"18/12/2013 19:53", "r", 5} and replace all strings with the format matching x/x with a format of xx-" The part after /. I'm a bit hazy on what it means or what I'm doing. I'd like to understand properly. Thanks for the help. –  Lara Jordan Oct 15 '14 at 22:07
I just tried it but I don't know what to put instead of the ee-. I'm trying to replace my date of this format 18/12/2013 18:53 with a date of this format 2013-12-18 hh24:mm:ss (where the ss will just be 00). –  Lara Jordan Oct 15 '14 at 22:15
Is all of your data dates Strings? –  Algohi Oct 15 '14 at 22:22

Assuming that you want strings as the output, this is an approach that is completely customizable because it applies a converter function (convert) that is defined by your specifications. I start with some test data in m, and put the converted data in the list new:

m = {{4,8,-3,14,0},{12,7,11,1,-7}}
convert[number_] :=
If[Abs[number] < 10, StringInsert[ToString[number], "0", -2],
ToString[number], number]

new = Map[convert, m, {-1}]

(*
==> {{"04", "08", "-03", "14", "00"},
{"12", "07", "11", "01", "-07"}}
*)


The result is a list in which all numbers have been converted to strings. In the Map function that creates the result, the level specification {-1} means that the convert function is applied to the individual elements of the list m.

The If statement in convert inserts a string "0" only if the number is single-digit. If convert is applied to something that's not a number, it returns the unchanged argument.

-