Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
so you want to convert numbers to strings ... –  belisarius Jun 4 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 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 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 at 18:04
add comment

4 Answers 4

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

enter image description here

share|improve this answer
add comment

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]]

enter image description here

Note:

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

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

This may be easier.

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

enter image description here

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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