Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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"}}
share|improve this question
1  
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. –  Liam William Aug 13 '13 at 16:36
add comment

2 Answers 2

up vote 1 down vote accepted

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.}
share|improve this answer
    
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? –  AteTheSputnik Aug 15 '13 at 6:18
    
@AteTheSputnik Could you please give me a small example of your table with transposed input? (You can edit the question to add it.) –  Mr.Wizard Aug 15 '13 at 6:31
add comment

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
*) 
share|improve this answer
    
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. –  AteTheSputnik Aug 16 '13 at 19:26
    
@AteTheSputnik yes, it is reasonable to apply this approach in case you need to have the same format for all the data. –  Alexei Boulbitch Aug 19 '13 at 6:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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