2
$\begingroup$

what can I do to have Mathematica intepret and show a number with exactly the amount of digits I gave as an input? I have a larger talbe with numbers (text file, whitespace seperated) which are formatted in the way that the prescission is given by the number of digits. To I can't work around using the same number of digits for the whole input.

For example if I give the input

In: 2.414000
    2.550

Mathematica will retun

Out1: 2.414
Out2: 2.55

I would instead like to exactly the nuber, or more precisely the amount of digits I had as an input. So in this case I would like to have see the number including the three zeros, or one zero respectively at the end of them.

Here one addition: I very much like the appraoch that was proposed to import strings and convert them! But what will I need to change to convert for example this table here for calculations? I imported it as strings before.

\begin{array}{lrr} \text{En[MeV]} & 0 & 1 \\ 0 & .0317223 & .0237898 \\ 1 & .1717071 & .1555525 \\ <\nu > & 22.4140000 & 2.5236700 \\ \text{$<\nu $($\nu $-1)$>$} & 4.6382 & 5.1013758 \\ \text{$<\nu $($\nu $-1)($\nu $-2)$>$} & 6.8176 & 8.0012201 \\ \end{array}

Or given as a List:

{{"En[MeV]", "0", "1"}, {"0", ".0317223", ".0237898"}, {"1", ".1717071", ".1555525"}, {"<\[Nu]>", "22.4140000", "2.5236700"}, {"<\[Nu](\[Nu]-1)>", "4.6382", "5.1013758"}, "<\[Nu](\[Nu]-1)(\[Nu]-2)>", "6.8176", "8.0012201"}}
$\endgroup$
1
  • 1
    $\begingroup$ At least from what I understand you will have to store the number as a string if you want to store the exact input value that is entered. $\endgroup$
    – William
    Aug 13, 2013 at 16:36

2 Answers 2

1
$\begingroup$

There are other questions that deal with the specifics of Mathematica arbitrary-precision syntax and functionality. I shall focus only on the question at had: inputting numbers.

You can import your numbers as Strings and then process the strings to produce the correct arbitrary precision syntax.

strings = ImportString["2.414000\n2.550", "Words"]

StringReplace[strings,
  n : NumberString :>
   ToExpression[n ~~ "`" ~~ ToString[StringLength[n] - 1]]
] /. _[x_] :> x

% // InputForm
{"2.414000", "2.550"}

{2.414000, 2.550}

{2.414`7., 2.55`4.}
$\endgroup$
2
  • $\begingroup$ I still find it puzzling that Mathematica should not have such a function. Well, it works for now. One additional question. If I want to replace a table containing all these strings (because I worked on them and transposed the input), where second input line will I need to place it/what will I have to change? $\endgroup$ Aug 15, 2013 at 6:18
  • $\begingroup$ @AteTheSputnik Could you please give me a small example of your table with transposed input? (You can edit the question to add it.) $\endgroup$
    – Mr.Wizard
    Aug 15, 2013 at 6:31
0
$\begingroup$

There is a way of doing this using the NumberForm[ ] operator. Like in the following:

This:

    a = 2.414000
b = 2.550

are your numbers. This:

 NumberForm[a, {5, 6}]
NumberForm[b, {4, 3}]

 (*   \!\(
    TagBox[
    InterpretationBox["\<\"2.414000\"\>",
    2.414,
    AutoDelete->True],
    NumberForm[#, {5, 6}]& ]\)

    \!\(
    TagBox[
    InterpretationBox["\<\"2.550\"\>",
    2.55,
    AutoDelete->True],
    NumberForm[#, {4, 3}]& ]\)

*)

is your solution ready to be shown. Copy-paste it into your notebook, or evaluate the NumberForm operators above. Just play with the numbers in the curly brackets.

The solution has a drawback. It is only to be shown as an end-result. It is not for further operation. You may make sure by evaluating this:

    Head[b]
Head[NumberForm[b, {4, 3}]]

(* Real

NumberForm  *)

As you see its head is NumberForm, rather than Real. It is for this reason you see these strange outputs above. So with the results of the NumberForm one cannot operate directly. There is a workaround, however, since the first part of the NumberForm is the initial number:

    NumberForm[b, {4, 3}][[1]]
% // Head

    (*
    2.55

    Real
*) 
$\endgroup$
2
  • $\begingroup$ Thanks for the effort, however this method seems not very useful when applying to a larger amount of data, as, if I understand it correctly, I would need to specify manually the numberform of each number. $\endgroup$ Aug 16, 2013 at 19:26
  • $\begingroup$ @AteTheSputnik yes, it is reasonable to apply this approach in case you need to have the same format for all the data. $\endgroup$ Aug 19, 2013 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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