0
$\begingroup$

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.

$\endgroup$

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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]
$\endgroup$
7
  • $\begingroup$ 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 $\endgroup$
    – niren
    Apr 24, 2013 at 9:59
  • $\begingroup$ @niren Indeed but you didn't ask that ... $\endgroup$
    – Spawn1701D
    Apr 24, 2013 at 10:06
  • $\begingroup$ @niren This is closer to what you really want: InputField[Dynamic[ToString@NumberForm[b, {4, 2}]], String] $\endgroup$
    – Spawn1701D
    Apr 24, 2013 at 10:11
  • $\begingroup$ I want only Number in InputField, Since the result from the InputField will go for further calculation. $\endgroup$
    – niren
    Apr 24, 2013 at 10:16
  • $\begingroup$ @niren but inside the input field you cant do any calculations just input a number. You have to explain exactly what you want. $\endgroup$
    – Spawn1701D
    Apr 24, 2013 at 10:19

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.