Thread Subject: Precision

Subject: Precision

From: Sudipta Basu

Date: 2 Jun, 2009 19:27:01

Message: 1 of 6

Can anyone suggest me how to set the precision of a give number in MATLAB, something like there is number say n=5.4356 which is calculated by a program. Now I would like to see the result upto say 2 places of decimal i.e.5.44. How can I do this in MATLAB?

Subject: Precision

From: John

Date: 2 Jun, 2009 20:25:03

Message: 2 of 6

Try sprintf('%.2f',n) . This will format and rount it to two places of decimal, but the value returned is a string.



"Sudipta Basu" <tupai_basu2004@yahoo.com> wrote in message <h03ue5$mni$1@fred.mathworks.com>...
> Can anyone suggest me how to set the precision of a give number in MATLAB, something like there is number say n=5.4356 which is calculated by a program. Now I would like to see the result upto say 2 places of decimal i.e.5.44. How can I do this in MATLAB?

Subject: Precision

From: someone

Date: 2 Jun, 2009 21:25:03

Message: 3 of 6

"Sudipta Basu" <tupai_basu2004@yahoo.com> wrote in message <h03ue5$mni$1@fred.mathworks.com>...
> Can anyone suggest me how to set the precision of a give number in MATLAB, something like there is number say n=5.4356 which is calculated by a program. Now I would like to see the result upto say 2 places of decimal i.e.5.44. How can I do this in MATLAB?

% Because of floating point precision,
% you can't do this EXACTLY, but you can come close...

% For example:
round(100*n)/100
% will "sorta" round n to two decimal places.
% 5.1234 -> 5.12 may actualy be stored as 5.119999...

% Another option is something like:
str2num(num2str(n,2))
% to get n to 2 significant digits.
% Same floating point precision problem again.

% See Q6.1 of the MATLAB FAQ at:
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Precision

From: Marcus M. Edvall

Date: 3 Jun, 2009 02:46:46

Message: 4 of 6

Hi Sudipta,

You can execute this in Matlab:

>> format bank
>> n=5.4356

n =

          5.44

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomopt.com/
http://tomsym.com/

Subject: Precision

From: Sudipta Basu

Date: 3 Jun, 2009 03:26:01

Message: 5 of 6

"Marcus M. Edvall" <edvall@gmail.com> wrote in message <e53d7a93-1b49-4b17-8906-9a19859bea93@h23g2000vbc.googlegroups.com>...
> Hi Sudipta,
>
> You can execute this in Matlab:
>
> >> format bank
> >> n=5.4356
>
> n =
>
> 5.44
>
> Best wishes, Marcus
> Tomlab Optimization Inc.
> http://tomopt.com/
> http://tomsym.com/

Thanx Marcus. Your suggestion has been quite helpful to me.

Subject: Precision

From: Steven Lord

Date: 3 Jun, 2009 14:07:05

Message: 6 of 6


"Sudipta Basu" <tupai_basu2004@yahoo.com> wrote in message
news:h04qg9$6gn$1@fred.mathworks.com...
> "Marcus M. Edvall" <edvall@gmail.com> wrote in message
> <e53d7a93-1b49-4b17-8906-9a19859bea93@h23g2000vbc.googlegroups.com>...
>> Hi Sudipta,
>>
>> You can execute this in Matlab:
>>
>> >> format bank
>> >> n=5.4356
>>
>> n =
>>
>> 5.44
>>
>> Best wishes, Marcus
>> Tomlab Optimization Inc.
>> http://tomopt.com/
>> http://tomsym.com/
>
> Thanx Marcus. Your suggestion has been quite helpful to me.

Be careful. The code Marcus gave you simply changes how the number is
_displayed_. It has no effect on how the number is _stored_ -- any
calculations that you perform later on will still use the full value of n,
not just 5.44.

The approach "someone" described earlier in this thread rounds the value of
the number, not just how it's displayed, but as he or she says there are
some caveats of which you should be aware. I second his/her recommendation
to read question 6.1 in the newsgroup FAQ (and the documents linked at the
end of that answer.)

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
sprintf John 2 Jun, 2009 16:29:04
rssFeed for this Thread

Contact us at files@mathworks.com