Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: decimal precision

Subject: decimal precision

From: Young Ryu

Date: 13 May, 2008 18:31:08

Message: 1 of 5

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!

Subject: Re: decimal precision

From: jay vaughan

Date: 13 May, 2008 18:43:03

Message: 2 of 5

"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!

n = 1.41536665;
% you could multiply, round, then divide
num_dig = 4;
n_rounded = round(n*(10^num_dig))/(10^num_dig);

% or for display purposes only you can use
num2str(n,'%.4f')

Subject: Re: decimal precision

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 13 May, 2008 18:57:24

Message: 3 of 5

In article <g0cmpc$gjg$1@fred.mathworks.com>,
Young Ryu <ryuyr77@gmail.com> wrote:

>I'd like to round at 4th decimal point. For example,

>1.41536664
>=> 1.415

>how can I round at specific precision?

It is not (generally) possible to round fractions at a decimal precision,
The exceptions have to do with rounding to integers divided by exact powers
of 2, such as 1/2, 1/4, 3/4, and so on.

There is no exact representation in binary floating point for 0.1 (1/10),
just as there is no exact representation in base 10 decimal expression
for 1/3. If your number does not happen to round to one of those
power-of-2 fractions I mentioned before, then there will always be
something left over -- it might be 15 decimal places further down, but
it will be there.

You -can-, though, convert numbers to character strings that
have specific numbers of decimal places, for "presentation purposes"
as the other poster mentioned.
--
  "He wove a great web of knowledge, linking everything together,
  and sat modestly at a switchboard at the center, eager to help."
                                              -- Walter Kerr

Subject: Re: decimal precision

From: Roger Stafford

Date: 13 May, 2008 19:22:03

Message: 4 of 5

"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

Subject: Re: decimal precision

From: Roger Stafford

Date: 14 May, 2008 00:24:01

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <g0cpor$h3v$1@fred.mathworks.com>...
> 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
------------
  I was hasty in my earlier response and made three errors. I should have
said:

1) To display n to four significant decimals, do:

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

2) To actually alter the number n to the nearest four place decimal, do:

 m = 4; % Number of significant decimals
 k = floor(log10(abs(n)))-m+1;
 n2 = round(n/10^k)*10^k;

  a) The most serious error I made was multiplying n by 10^k instead of
dividing by it. That really makes a mess of it!

  b) However, using 'fix' instead of 'floor' is also an error, giving one too few
significant decimal places for numbers less than 1.

  c) In 1) with %12.4e it would result in 5, not 4, significant decimals being
displayed.

  Sorry about all that! Perhaps I stayed up too late last night.

Roger Stafford

Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics