35
$\begingroup$

The best way I can come up with to say "show me this fraction as a decimal number to M places, don't use scientific notation" is:

NumberForm[N[1/998001,2994],ExponentFunction->(Null&)]

It seems like that's an awful lot of typing for a very simple request.

Is it possible to say it more concisely?

$\endgroup$
3
  • $\begingroup$ I think that is the canonical way, but that doesn't mean there isn't something more terse. $\endgroup$
    – Mr.Wizard
    Commented Jan 26, 2012 at 19:42
  • 2
    $\begingroup$ I don't think you can avoid it altogether. But if you find yourself doing it a lot, you can always use SetOptions... $\endgroup$ Commented Jan 26, 2012 at 19:53
  • $\begingroup$ I can't use any of these values to plot becasue their Head changes to NumberForm and graphs don't understand this form. I need to plot and have the tooltip show decimal form, any suggested alterations to the above to make it work? $\endgroup$
    – lara
    Commented Apr 10, 2013 at 7:24

3 Answers 3

24
$\begingroup$

You can express any fraction/number to arbitrary decimal places by using a backtick followed by number of digits required. For example:

In[1]:= 4/3`20
Out[1]= 1.3333333333333333333

This is the same as N[4/3, 20]. Now combine this with AccountingForm, which never uses scientific notation to get the output that you desire.

AccountingForm[1/998001`2994]
Out[2]//AccountingForm= 0.0000010020030040050060070080090100110120130140...

However, be aware that AccountingForm uses parentheses for negative numbers:

AccountingForm[-1/998001`2994]
Out[3]//AccountingForm= (0.00000100200300400500600700800901001101201301401501601....

Daniel Lichtblau has a good point that although using `instead of N might be shorter in this case, in general, it might not give the same result — for example, compare the digits of Log[2`50] and N[Log[2],50]. You'll see that they differ in the last couple of digits. However, for small use cases, the difference might be insignificant.

$\endgroup$
6
  • $\begingroup$ I also thought of AccountingForm (and RealDigits), but I think the negative numbers are an issue. $\endgroup$
    – Mr.Wizard
    Commented Jan 26, 2012 at 19:59
  • $\begingroup$ @Mr.Wizard Yes, but I also noticed that it uses () for negative numbers, which could be used to distinguish if one were to use this only for quick visual checking $\endgroup$
    – rm -rf
    Commented Jan 26, 2012 at 20:01
  • $\begingroup$ I am voting for this since I think AccountingForm comes closest. $\endgroup$
    – Mr.Wizard
    Commented Jan 26, 2012 at 21:16
  • 2
    $\begingroup$ Might just use N[...,m]. Check difference between Log[2`20] and N[Log[2],20]. The former will lose a digit. (Might not be important for the purposes at hand, though.) $\endgroup$ Commented Jan 27, 2012 at 0:09
  • 2
    $\begingroup$ Of course those Log calculations give different answers. The former is Log[(2 with 50 digit precision)], the latter is (Log[2] with 50 digit precision). Taking Log[2`50] and Log[N[2,50]] gives exactly the same answer. $\endgroup$
    – Jacob Bond
    Commented Nov 11, 2015 at 21:15
23
$\begingroup$

You could always set $Post to have this happen automatically.

format[x_Real] := NumberForm[x, ExponentFunction -> (Null &)];
format[x_] := x;
$Post = format;

Now,

N[1/998001, 50]

returns

0.0000010020030040050060070080090100110120130140150160170

Even better, $Post is applied at display time, thus

Head[%]

returns Real.

$\endgroup$
3
$\begingroup$

This post got many upvotes now. But no one mentioned DecimalForm which is introduced in ver 11.2, 2017

DecimalForm[N[1/998001, 2994]]

gives exactly what you need.

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