1
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ so you want to convert numbers to strings ... $\endgroup$ Jun 4, 2014 at 18:07
  • $\begingroup$ 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]. $\endgroup$
    – Szabolcs
    Jun 4, 2014 at 18:09
  • $\begingroup$ Presumably, it is to align elements in a Table or Grid, e.g., list = RandomInteger[{1, 20}, {10, 3}]; IntegerString[list, 10, 2] // Grid $\endgroup$
    – Bob Hanlon
    Jun 4, 2014 at 19:12
  • $\begingroup$ 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. $\endgroup$
    – bernie
    Jun 10, 2014 at 18:04

4 Answers 4

3
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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.

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

This may be easier.

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

enter image description here

$\endgroup$
12
  • $\begingroup$ 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? $\endgroup$
    – lara
    Oct 15, 2014 at 5:58
  • $\begingroup$ is this good for you: {"18/12/2013 19:53", "r", 5} /. _String?(StringMatchQ[#, ___ ~~ "/" ~~ ___] &) :> ee $\endgroup$ Oct 15, 2014 at 19:52
  • $\begingroup$ 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. $\endgroup$
    – lara
    Oct 15, 2014 at 22:07
  • $\begingroup$ 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). $\endgroup$
    – lara
    Oct 15, 2014 at 22:15
  • $\begingroup$ Is all of your data dates Strings? $\endgroup$ Oct 15, 2014 at 22:22
0
$\begingroup$

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.

$\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.