Path: news.mathworks.com!not-for-mail
From: "us " <us@neurol.unizh.ch>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to round to 2 decimal?
Date: Wed, 5 Mar 2008 09:58:02 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 20
Message-ID: <fqlqra$c3b$1@fred.mathworks.com>
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>
Reply-To: "us " <us@neurol.unizh.ch>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1204711082 12395 172.30.248.35 (5 Mar 2008 09:58:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Mar 2008 09:58:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:455456


"nor ki":
<SNIP more on FP-trash...

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

does it?

     format long;
     r=str2num(sprintf('%5.2f',pi))
% r =
%     3.140000000000000
% however,
     s=sprintf('%.20f\n',r,3.14)
% s = 
%     3.14000000000000010000 % <- result
%     3.14000000000000010000 % <- hand coded

us