1
$\begingroup$

I'd like to define a function, call it NumberF, that takes a real number n and two naturals l and r so that NumberF[n,l,r] yields the real number n with l and r zeroes on the left and right of the number respectively, where the right one are within decimal places. For example,

NumberF[10,0,1] == 10.0
NumberF[10,1,3] == 010.000
NumberF[10,3,0] == 00010
NumberF[3.45,4,2] == 00003.4500
NumberF[1003.400405,2,3] == 001003.400405000

The output could be a string. I know about NumberForm, which does part of it, as well as IntegerString. I might be able to do it by combining both, but I was wondering if there is a better way of doing this. Any ideas?

Edit: What about a function NumberF2 that receives similar input, but now it completes the number with zeroes up to the lth and rth position (with respect to the decimal divider). For example,

NumberF[10.34,0,2] == 10.34
NumberF[10.3,0,2] == 10.30
NumberF[3.45,4,2] == 0003.45
NumberF[1234.5678,2,3] == 1234.5678
$\endgroup$
3
  • $\begingroup$ For your application, is it important to have decimal point alignment if many numbers are printed out as a table? $\endgroup$
    – Syed
    Sep 5 at 5:05
  • $\begingroup$ @Syed not sure I understand the question. Can you explain a bit more? $\endgroup$
    – sam wolfe
    Sep 5 at 15:46
  • $\begingroup$ If many such numbers are collected (with different zeros on either side) over a period of time, and you want to print these out, how would you further align these on a table now that you have a string. I think decimal would align but then there will be different gaps on either side for each differently formatted set of numbers. Having only real numbers, you would have to do this only at the time of printing. That's why I asked the question, which I think failed to convey the full extent of what I was thinking. Also what will happen to the sign of the number? $\endgroup$
    – Syed
    Sep 5 at 16:26
1
$\begingroup$

Something like this, for instance:

numberF[a_?NumericQ, n_Integer /; n > 0, m_Integer /; m > 0] := StringJoin[StringRepeat["0", n], ToString[N@a, InputForm], StringRepeat["0", m]]
numberF[a_?NumericQ, 0, m_Integer /; m > 0] := StringJoin[ToString[N@a, InputForm], StringRepeat["0", m]]
numberF[a_?NumericQ, n_Integer /; n > 0, 0] := StringJoin[StringRepeat["0", n], ToString@a]

For the second edited question, try

numberF[num_?NumericQ, n_Integer, m_Integer] := Module[
  {str = StringSplit[ToString[N@num, InputForm], "."], len}
  , If[Length@str == 1 && m == 0
   , StringPadLeft[str[[1]], Max[n, StringLength@str[[1]]], "0"]
   , If[Length@str == 1, str = Append[str, "0"]]
   ; StringPadLeft[str[[1]], Max[n, StringLength@str[[1]]], "0"] <> "." <> StringPadRight[str[[2]], Max[m, StringLength@str[[2]]], "0"]
   ]
  ]
$\endgroup$
3
  • $\begingroup$ Thank you so much, this works! However, I was wondering if I could change it a bit (I actually wanted another function initially, which is more useful to me, but I described it wrongly and forgot about some particular cases). I've edited the question to add this alternate function. Perhaps slightly trickier, but could you please take a look at it? $\endgroup$
    – sam wolfe
    Sep 5 at 16:06
  • $\begingroup$ @samwolfe. Try the updated version. $\endgroup$
    – march
    Sep 5 at 16:56
  • $\begingroup$ Could you please add some test cases to show the intended use as requested in the title? $\endgroup$
    – Syed
    Sep 6 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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