Path: news.mathworks.com!not-for-mail
From: "nor ki" <kinor.removeme@gmx.de>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to round to 2 decimal?
Date: Wed, 5 Mar 2008 08:28:01 +0000 (UTC)
Organization: fz
Lines: 38
Message-ID: <fqllih$doa$1@fred.mathworks.com>
References: <fqjpma$66c$1@fred.mathworks.com> <fqkn97$hnn$1@fred.mathworks.com> <fqkpbj$72h$1@canopus.cc.umanitoba.ca>
Reply-To: "nor ki" <kinor.removeme@gmx.de>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1204705681 14090 172.30.248.37 (5 Mar 2008 08:28:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Mar 2008 08:28:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1088664
Xref: news.mathworks.com comp.soft-sys.matlab:455449


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqkpbj$72h$1@canopus.cc.umanitoba.ca>...
> In article <fqkn97$hnn$1@fred.mathworks.com>, Dan Haeg
<haegd@msoe.edu> wrote:
> >"Deo " <spliers@hotmail.com> wrote in message
> ><fqjpma$66c$1@fred.mathworks.com>...
> >> hey, how can I set my matlab program to round all the 
> >> double numbers to 2 decimals
> 
> >here is one way:
> >
> >data=round(data*100)/100
> 
> >> data=pi; data=round(data*100)/100; sprintf('%.60g', data)
> 
> ans =
> 
> 3.140000000000000124344978758017532527446746826171875
> 
> Why didn't I get 3.14 exactly? I followed your steps word
for word!
> Is sprintf broken???
> -- 
>   "All human knowledge takes the form of interpretation."
>                                               -- Walter
Benjamin

Hi Benjamin,

no sprintf is fine the problem occurs by /100. as 100 is no
power of 2 rounding errors occur
this will always occur when you divide.

for an exact rounding try 
str2num(sprintf('%5.2f',pi))
which looks strange to me but seems to work

kinor