Path: news.mathworks.com!newsfeed-00.mathworks.com!NNTP.WPI.EDU!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to round to 2 decimal?
Date: Wed, 5 Mar 2008 17:36:28 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 60
Message-ID: <fqmlms$47v$1@canopus.cc.umanitoba.ca>
References: <fqjpma$66c$1@fred.mathworks.com> <fqkn97$hnn$1@fred.mathworks.com> <fqkpbj$72h$1@canopus.cc.umanitoba.ca> <fqllih$doa$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1204738588 4351 192.70.172.160 (5 Mar 2008 17:36:28 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Wed, 5 Mar 2008 17:36:28 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:455547


In article <fqllih$doa$1@fred.mathworks.com>,
nor ki <kinor.removeme@gmx.de> wrote:
>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???

>no sprintf is fine the problem occurs by /100. as 100 is no
>power of 2 rounding errors occur

But Dan said that his way worked! Are you saying that he was WRONG?

>this will always occur when you divide.

>> data=3.25;data=round(data*100)/100; sprintf('%.60g', data)

ans =

3.25

Where did the round-off error in Dan's routine go? I can't find it?
Do I need a wider format than %.60g to see it??


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

>> data=str2num(sprintf('%5.2f',pi)); sprintf('%.60g', data)

ans =

3.140000000000000124344978758017532527446746826171875

Why didn't I get 3.14 exactly? I followed your steps word for
word! Is sprintf broken???

-- 
  "Do diddle di do,
   Poor Jim Jay
   Got stuck fast
   In Yesterday."                             -- Walter De La Mare