# Constructing a List from a Decimal Number

I am trying to get from a number such as $12.345$ to a list $\{1,2,3,4,5\}$. My best attempt so far has been:

First[RealDigits[12.345]]


however this of course gives $\{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\}$ since it assumes the number to have infinite precision.

Any help is greatly appreciated

• First[RealDigits[Rationalize[12.345]]]? – J. M.'s technical difficulties May 21 '17 at 18:44
• What output do you expect for 100.00 ? – Shadowray May 21 '17 at 19:00
• Related: (110596) – Mr.Wizard Jul 21 '18 at 3:13

12.345 does not have infinite precision, rather it has machine precision. If you specify the precision correctly your formulation will work as written:

First @ RealDigits[12.3455]

{1, 2, 3, 4, 5}


I believe a modified input form like this is necessary to remove ambiguity; see:

J. M. proposed Rationalize in a comment but this cannot be relied upon, e.g.:

First @ RealDigits @ Rationalize[12.345678]

{1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0}


First @ RealDigits[12.345678] /. {a___, 0 ...} :> {a}

{1, 2, 3, 4, 5, 6, 7, 8}

• Hi Mr.W. Could not respond to your comment re my deleted answer. I deleted it because it cannot handle 100.00 (which, I presumed, should be transformed to {1, 0, 0, 0, 0}). – kglr May 21 '17 at 19:11
• @kglr I see. But I think that is a problem with the OP's input format, i.e. it is an unreasonable expectation. We could perhaps drop only zeros to the right of the decimal point, but there is no way to differentiate 100.00 from 100.00000` etc.; the precision must be included in the input I believe. – Mr.Wizard May 21 '17 at 19:14