3
$\begingroup$

I would like to remove the leading zero and trailing decimal from the display of numbers in plots. For example, to get the correlation values to fit inside the cells of a correlation matrix, I can free up some space by removing all the leading zeros. So instead of 0.14 I would like .14 and instead of -0.28 I would like -.28. I know that's not good formatting for the text of a paper, but for a plot this is more desirable in this case.

I would also like to remove the "." after 1..

I thought there would be options for this kind of output for NumberForm, but they are not documented if they do exist. Currently I'm using this formatting function:

FormatCorrelations[n_] := If[NumberQ[n], NumberForm[n, {2, 2}, ExponentFunction -> (Null &), NumberPadding -> {"", ""}], n];

I would like to keep all those options (specifying the significance and number of decimals, preventing exponential notation, and ignoring string inputs) while additionally removing the leading zeros and trailing decimals.

I found one answer for the trailing decimal in this questions, but it doesn't work me (breaks the code).

I also found this answer to remove the trailing decimal: NumberForm[# /. x_ /; x == 1 -> 1, 2], but it fails to remove the decimal when I apply it via my function.

$\endgroup$
3
  • $\begingroup$ why should 1. be formatted differently than 1.0001? Because the former will be 1 and the latter 1.00. $\endgroup$
    – Kuba
    Commented Aug 9, 2017 at 9:00
  • $\begingroup$ @Kuba: it's true that 1. -> 1 and 1.0001 -> 1.00 according to what I'm looking for. That may be inappropriate for some uses, but I am only using this formatting for proportions and correlations, so it is not an issue for me. $\endgroup$ Commented Aug 9, 2017 at 9:04
  • $\begingroup$ For formatting integers, see this. $\endgroup$ Commented Aug 9, 2017 at 9:16

2 Answers 2

2
$\begingroup$

Here is an extremely inelegant way of doing this:

FormatCorrelations[n_]:=Module[{TheNumber},
  TheNumber=ToString[If[NumberQ[n],If[n==1||n==1.,1,NumberForm[n,{2,2},ExponentFunction->(Null&),NumberPadding->{"",""}]],n]];
  TheNumber=If[StringLength[TheNumber]>3,If[StringTake[TheNumber,3]=="-0.","-."<>StringDrop[TheNumber,3],TheNumber],TheNumber];
  TheNumber=If[StringLength[TheNumber]>2,If[StringTake[TheNumber,2]=="0.","."<>StringDrop[TheNumber,2],TheNumber],TheNumber];
  TheNumber
];

Because I am just overlaying the numbers on a correlation matrix array plot, this string-based approach works for me. Hopefully it will be useful to other people as well. I suspect there is a better (faster and with less code) way of doing this conversion, but one nice thing about this overly verbose code is that it would be easy for people to understand and customize to their own needs.

$\endgroup$
1
$\begingroup$

I would suggest

reform[x_String] := x
reform[1 | 1.] := "1"
reform[0 | 0.] := "0"

reform[n_] :=
 With[{x = 
  ToString@NumberForm[n, {2, 2}, ExponentFunction -> (Null&), NumberPadding -> {"", ""}]},
   StringReplace[x, "0." -> "."]]

reform /@ {"abc", 0.0, 1, 1.001, -0.12, 0.12, 10^6}

{"abc", "0", "1", "1.", "-.12", ".12", "100000."}

$\endgroup$
1
  • $\begingroup$ That mostly does the same thing as my solution, although I guess it's not really shorter or more elegant. Note that if you feed 100. into this it reports 10. instead of 100 which is both missing a zero and not in line with the spirit of the intended simplified formatting. Similarly, -100.0 returns -10. instead of -100. This issue is why I checked StringTake[TheNumber,2] with my approach. $\endgroup$ Commented Aug 10, 2017 at 11:50

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.