27
$\begingroup$

I have a lot of data to inspect. An example of a number in my program is

123.189094

This gets displayed as

123.189

Unfortunately, most of my information is in the small digits, so, when visually inspecting this data, 123.189094 and 123.189263 are not distinguishable, in other words, a list like

{123.189094, 123.189263}

is displayed like

{123.189, 123.189}

and it looks like I have equal data when I don't. The only fix I have found for this is FullForm, but that explodes out all the structure (lists, rules, etc.) and makes the display very annoying. Imagine that my data were actually buried deep in some nested structure of lists and rules (and it is).

{123.189094, 123.189263}//FullForm
(* List[123.189094`,123.189263`] *)

is not what I want. I would have thought N[#,10]& or somesuch would work, but it doesn't

{123.189094, 123.189263} // N[#, 10] &
(* {123.189,123.189} AAACH! *)

StandardForm doesn't do it, TraditionalForm doesn't do it.

I'm stuck. As usual, will be grateful for any advice or ideas.

$\endgroup$
2
  • 1
    $\begingroup$ Related: Mathematica Precision $\endgroup$ Commented Mar 30, 2012 at 23:16
  • 6
    $\begingroup$ It is possible to set the number of digits in Preferences->Appearance->Numbers->Formatting $\endgroup$
    – Andrew
    Commented Mar 31, 2012 at 7:34

6 Answers 6

21
$\begingroup$

Maybe this :

NumberForm[#, 10] &@ {123.189094`, 123.189263`}
{123.189094, 123.189263 }

?

Edit

Consider also this utility of NumberForm[ x, {m, k}] giving m real digits of x with k digits to the right of the decimal point, e.g.

NumberForm[#, {10, 7}] &@ { 197.9898987322333, 201.73205080756887 }
{ 197.9898987, 201.7320508 }
$\endgroup$
4
  • $\begingroup$ But if I have a list of numbers, after applying NumberForm, I can't access the list elements as usual, but with an extra index, for example what was before mylist[[1]] is now mylist[[1]][[1]]. How can avoid this? I need to pass this list to a function. $\endgroup$
    – Santiago
    Commented Apr 24, 2014 at 10:09
  • $\begingroup$ I would make it like this f[ NumberForm[#, {10, 7}]& @ list]. $\endgroup$
    – Artes
    Commented Apr 24, 2014 at 10:55
  • $\begingroup$ Sorry @Artes, but that still doesn't work. NumberForm returns something that has not the same structure as my original list. Using First[NumberForm[#, {10, 7}]& @ list] returns something that still has other structure, with a list . And then if I do First[First[NumberForm[#, {10, 7}]& @ list]] it returns only a list with the original "old" precision digits. $\endgroup$
    – Santiago
    Commented Apr 29, 2014 at 16:18
  • $\begingroup$ @Santi If you have some problems you should ask another question checking if there is no one similar. Here I solved the OP problem. Why should I bother some unclear questions? I believe you would rather use something different but there is no precise information. Perhaps you should check RootApproximant or with the second argument Rationalize or Chop or Round or ... $\endgroup$
    – Artes
    Commented Apr 29, 2014 at 17:10
33
$\begingroup$

If this is something you want in general, try:

SetOptions[$FrontEnd, PrintPrecision-> 10]

and if you just want it for a specific notebook, then do:

SetOptions[InputNotebook[], PrintPrecision-> 10]
$\endgroup$
1
  • 2
    $\begingroup$ I have just seen that this was asked again. As this is a question which presumably beginners are asking I think it would make sense to mention that you can change that setting in the "Preferences" dialog (menu entry "Edit" -> "Preferences") in the Numbers tab of the Appearance tab... $\endgroup$ Commented Jan 18, 2016 at 17:44
28
$\begingroup$

PrintPrecision

You can control the number of digits displayed using the PrintPrecision option.

You have a number of options for its use. You can set it Globally or for the specific Notebook using the Options Inspector. You can also use it directly with Style:

Style[123.189094, PrintPrecision -> 10]
123.189094

You can set it temporarily for one session like this:

SetOptions[$FrontEndSession, PrintPrecision -> 10]

Finally you can set it using Style Sheets (select cell type Output).


Arbitrary precision arithmetic

If you use arbitrary precision arithmetic Mathematica automatically displays all digits. If you are concerned with the small end of numbers you should probably be using arbitrary precision anyway. Please see the second half of this answer for more.

123.189094`9
123.189094
$\endgroup$
2
  • $\begingroup$ No upvotes? I'll help you play ketchup. You and @RolfMertig. These are the answers, the other's are (some good) workarounds. It's not a question about precision and there's no need to wrap the result in some inert structure only for the FE to hide less of what it already received from the kernel $\endgroup$
    – Rojo
    Commented Mar 31, 2012 at 6:22
  • $\begingroup$ @Rojo thanks. By the way, please see my updated "Splitting" answer (the first one). I'm still chasing the perfection of doing it with a single pattern, but Mathematica is not yielding easily. $\endgroup$
    – Mr.Wizard
    Commented Mar 31, 2012 at 6:59
9
$\begingroup$

Use NumberForm:

NumberForm[{123.189094, 123.189263}, 9]
$\endgroup$
7
$\begingroup$

The other answers are fine, but if all the interest is in the small-end digits, consider something like:

((RealDigits /@ {123.189094`, 123.189263`}) /. 
  {a__, 0..} -> {a})[[All, 1, -4 ;; -1]]
{{9, 0, 9, 4}, {9, 2, 6, 3}}

This picks up the smallest-end non-zero digits so you can focus on them. This won't work if your numbers have a different number of digits. If that's the case, you will have to allow for this by instead dropping the first few digits from the RealDigits results and then removing the trailing zeros.

(RealDigits /@ {123.189094`, 123.189263`})[[All, 1, 5 ;; -1]] /. 
  {a__, 0 ..} -> {a}// TableForm
 8  9   0   9   4
 8  9   2   6   3
$\endgroup$
7
$\begingroup$

InputForm gives

  {123.189094, 123.189263} // InputForm
  (*==> {123.189094, 123.189263}  *)
$\endgroup$

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