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.

I have tried to resolve the problem of the following link How can I solve precision problem

I can tell the problem described in that link shortly here, It's no mater how many precision is there after decimal(.) the result should be 2 or 3 precision after decimal as per user requirement.e.g

N[10/3]
3

Output should be as per user wish precision as 2.

3.33
3.00

There I found NumberForm[] function as a solution, since the NumberForm[] function is wrap up the result, I can't get the result as number out of it.

I tried like this

NumberForm[N[10/3],{4,2}]
NumberForm[3, {4, 2}]

Output am getting is

3.33
3.00

But when I try to do further operation, it's not computing

NumberForm[N[10/3], {4, 2}] + 1
NumberForm[3, {4, 2}] + 1

Output

1+3.33
1+3.00

I tried other way also which follows

ToExpression[ToString[NumberForm[3, {4, 2}]]]

OutPut

3.

but I need output as 3.00. Help me to find the solution.

share|improve this question
add comment

2 Answers 2

Is SetAccuracy what you want?

a = N[10/3, {∞, 3}]

3.33

b = 3``3

3.00

a + 1

4.33

b + 1

4.00

394.985674``3

394.99

Please note Accuracy is a different concept from Precision.

share|improve this answer
add comment

Use the $PrePrint global parameter:

$PrePrint = If[MatchQ[#, _?NumericQ], NumberForm[#, {4, 2}], #] &;

Note: if you dont want the way rationals will be represented after setting the global variable (e.g. $3.00/4.00$) then use this instead

$PrePrint = If[MatchQ[#, Except[_Rational,_?NumericQ]], NumberForm[#, {4, 2}], #] &;

Update:

if you just want it for the InputField command try this instead

InputField[Dynamic[ToString@NumberForm[b, {4, 2}]], String]
share|improve this answer
    
I have problem with your answer, I need to give the result to InputField the output I got is not I expected.b=3 InputField[Dynamic[b], Number]. I got result 3 alone in InputField not 3.00 –  niren Apr 24 '13 at 9:59
    
@niren Indeed but you didn't ask that ... –  Spawn1701D Apr 24 '13 at 10:06
    
@niren This is closer to what you really want: InputField[Dynamic[ToString@NumberForm[b, {4, 2}]], String] –  Spawn1701D Apr 24 '13 at 10:11
    
I want only Number in InputField, Since the result from the InputField will go for further calculation. –  niren Apr 24 '13 at 10:16
    
@niren but inside the input field you cant do any calculations just input a number. You have to explain exactly what you want. –  Spawn1701D Apr 24 '13 at 10:19
show 2 more comments

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.