2
$\begingroup$

How could I convert a real number into a List of its digits in Mathematica?

Example:

8.168643234

Desired output:

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

Many thanks in advance!

$\endgroup$
3
  • 3
    $\begingroup$ First[RealDigits[x]]? $\endgroup$
    – Michael E2
    Jan 22 '15 at 21:19
  • $\begingroup$ @bbgodfrey thank you! I only just started working with Mathematica and I find it all brand new. I am a software engineer. I am looking to learn more about wolfram language! $\endgroup$
    – user1989
    Jan 22 '15 at 21:29
  • $\begingroup$ @MichaelE2 using your example I am not getting the output that I am looking for :s, thank you for trying! $\endgroup$
    – user1989
    Jan 22 '15 at 21:29
5
$\begingroup$

When you enter a number with a decimal point like:

x = 8.168643234;

you are telling Mathematica that this is a machine precision number. You can see this with:

Precision[x]
(* MachinePrecision *)

This means that the internal representation of the number is not exactly what you entered, but instead is the nearest machine real. RealDigits shows you the digits in the actual value of x:

First[RealDigits[x]]
(* {8, 1, 6, 8, 6, 4, 3, 2, 3, 3, 9, 9, 9, 9, 9, 9} *)

If you want to get the exact output in the question, you will need to either specify the precision of the number:

x = 8.168643234`10;

First[RealDigits[x]]
(* {8, 1, 6, 8, 6, 4, 3, 2, 3, 4} *)

or enter it as an exact (infinite precision) number:

x = 8168643234*10^-9;

First[RealDigits[x]]
(* {8, 1, 6, 8, 6, 4, 3, 2, 3, 4} *)
$\endgroup$
4
  • $\begingroup$ Thank you very much for your time answering my question. I appreciate your effort! $\endgroup$
    – user1989
    Jan 22 '15 at 21:48
  • $\begingroup$ @user1989, no problem. By the way it is usual here to wait a while before accepting an answer, so as not to discourage others from posting other, potentially better, answers. 24 hours is good as it gives the whole globe a chance to contribute. $\endgroup$ Jan 22 '15 at 21:53
  • $\begingroup$ I will keep that in mind! My apologies, reading through rules of this forum! $\endgroup$
    – user1989
    Jan 22 '15 at 22:25
  • 1
    $\begingroup$ @user1989, No apology necessary! It's not a rule, just a good tactic to get the best out of the site :-) $\endgroup$ Jan 22 '15 at 22:40

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.