Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: decimal precision
Date: Tue, 13 May 2008 19:22:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 39
Message-ID: <g0cpor$h3v$1@fred.mathworks.com>
References: <g0cmpc$gjg$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
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 1210706523 17535 172.30.248.35 (13 May 2008 19:22:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 13 May 2008 19:22:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:468231


"Young Ryu" <ryuyr77@gmail.com> wrote in message <g0cmpc$gjg
$1@fred.mathworks.com>...
> Hi-
> 
> I'd like to round at 4th decimal point. For example, 
> 
> 1.41536664
> => 1.415
> 
> how can I round at specific precision? There is no syntax  
> in "round" function.
> 
> Thanks!
----------
  You haven't made it clear whether it is only the display of a number or the 
actual number matlab is using in its computations that is to be altered.  There 
is an important difference between the two, though new users often do not 
distinguish between the two concepts.

  If you want to display a number to four significant figures, do this:

 fprintf('The number n equals: %12.4e\n',n)

  On the other hand, if you want to change the number actually stored in 
matlab in this manner, do this:

 m = 4; % Number of significant decimals
 k = fix(log10(abs(n)))-m+1;
 n2 = round(10^k*n)/10^k;
 fprintf('The altered number is: %30.20e\n',n2)
  = 1.41500000000000003600e+00

  You will note that if you display the altered number to a sufficient accuracy, 
it is unlikely to be exactly the fraction you requested.  That is because 
matlab's 'double' format is unable to exactly represent most decimal 
fractions.

Roger Stafford