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

Thread Subject: how to round to 2 decimal?

Subject: how to round to 2 decimal?

From: Deo

Date: 04 Mar, 2008 15:26:02

Message: 1 of 11

hey, how can I set my matlab program to round all the
double numbers to 2 decimals

Subject: Re: how to round to 2 decimal?

From: Dan Haeg

Date: 04 Mar, 2008 23:51:03

Message: 2 of 11

"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

rounding functions in matlab:

round
fix
floor
ceil

look in the help for more information.

Subject: Re: how to round to 2 decimal?

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

Date: 05 Mar, 2008 00:26:27

Message: 3 of 11

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

Subject: Re: how to round to 2 decimal?

From: nor ki

Date: 05 Mar, 2008 08:28:01

Message: 4 of 11

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

Subject: Re: how to round to 2 decimal?

From: us

Date: 05 Mar, 2008 09:58:02

Message: 5 of 11

"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

Subject: Re: how to round to 2 decimal?

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

Date: 05 Mar, 2008 17:36:28

Message: 6 of 11

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

Subject: Re: how to round to 2 decimal?

From: carlos lopez

Date: 05 Mar, 2008 23:30:18

Message: 7 of 11

"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
Hello Deo:
Aside from the other answers, I believe that you should
clearly state which one is your real wish:
a) perform every calculation in fixed point, using only two
decimals
b) display every final result as a real with just two decimals.
The other answers are related with the option b), but they
do not preclude that all the calculations will be carried
out to full double precision, and only later displayed as
you wish.
On the other hand, if you need something as described in a),
you should consider other approaches. To some extent, two
decimal arithmetic can be simulated with integer arithmetic
after multiplying the inputs by 100. Depending on your
matlab version, it will be simpler (or not) to achieve that.
Let us know about your option: namely a) or b)
Regards
Carlos

Subject: Re: how to round to 2 decimal?

From: Omar U. Florez

Date: 28 Jun, 2008 22:09:01

Message: 8 of 11

sprintf('%g',round(-sorted_values(j)*100)/100)

where the specifier '%g' avoids showing the last
or 'insignificant' zeros and shows only the two decimals
product of multiplying by 100.

>> sprintf('%g',round(pi*100)/100)

ans =
3.14

"carlos lopez" <clv2clv_00000000_@adinet.com.uy> wrote in
message <fqnaea$370$1@fred.mathworks.com>...
> "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
> Hello Deo:
> Aside from the other answers, I believe that you should
> clearly state which one is your real wish:
> a) perform every calculation in fixed point, using only
two
> decimals
> b) display every final result as a real with just two
decimals.
> The other answers are related with the option b), but they
> do not preclude that all the calculations will be carried
> out to full double precision, and only later displayed as
> you wish.
> On the other hand, if you need something as described in
a),
> you should consider other approaches. To some extent, two
> decimal arithmetic can be simulated with integer
arithmetic
> after multiplying the inputs by 100. Depending on your
> matlab version, it will be simpler (or not) to achieve
that.
> Let us know about your option: namely a) or b)
> Regards
> Carlos

Subject: Re: how to round to 2 decimal?

From: zia

Date: 28 Jun, 2008 22:18:35

Message: 9 of 11

On Jun 28, 6:09=A0pm, "Omar U. Florez" <omarflore...@gmail.com> wrote:
> sprintf('%g',round(-sorted_values(j)*100)/100)
>
> where the specifier '%g' avoids showing the last
> or 'insignificant' zeros and shows only the two decimals
> product of multiplying by 100.
>
> >> sprintf('%g',round(pi*100)/100)
>
> ans =3D
> 3.14
>
> "carlos lopez" <clv2clv_000000...@adinet.com.uy> wrote in
> message <fqnaea$37...@fred.mathworks.com>...
>
> > "Deo " <spli...@hotmail.com> wrote in message
> > <fqjpma$66...@fred.mathworks.com>...
> > > hey, how can I set my matlab program to round all the
> > > double numbers to 2 decimals
> > Hello Deo:
> > Aside from the other answers, I believe that you should
> > clearly state which one is your real wish:
> > a) perform every calculation in fixed point, using only
> two
> > decimals
> > b) display every final result as a real with just two
> decimals.
> > The other answers are related with the option b), but they
> > do not preclude that all the calculations will be carried
> > out to full double precision, and only later displayed as
> > you wish.
> > On the other hand, if you need something as described in
> a),
> > you should consider other approaches. To some extent, two
> > decimal arithmetic can be simulated with integer
> arithmetic
> > after multiplying the inputs by 100. Depending on your
> > matlab version, it will be simpler (or not) to achieve
> that.
> > Let us know about your option: namely a) or b)
> > Regards
> > Carlos


Hello,

Try this

x =3D 1.23456

quant(x,2)


function output =3D quant(number,digit)

output =3D round(number.*(10 ^ digit))./(10 ^ digit);


--Zia

Subject: Re: how to round to 2 decimal?

From: us

Date: 28 Jun, 2008 22:45:04

Message: 10 of 11

zia:
<SNIP another solution - another world...

> Try this
> x =3D 1.23456
> quant(x,2)

you should tell the OP that your solution requires the
neural network tbx...

us

Subject: Re: how to round to 2 decimal?

From: Nasser Abbasi

Date: 29 Jun, 2008 09:21:55

Message: 11 of 11


"us " <us@neurol.unizh.ch> wrote in message
news:g46etg$pmu$1@fred.mathworks.com...
> zia:
> <SNIP another solution - another world...
>
>> Try this
>> x =3D 1.23456
>> quant(x,2)
>
> you should tell the OP that your solution requires the
> neural network tbx...
>
> us

humm.. he pasted the one line source code for quant().

"function output = quant(number,digit)

output = round(number.*(10 ^ digit))./(10 ^ digit);"

So one can just copy it and use it. no need for neural netwrok toolbox?

Nasser



Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
precision carlos lopez 05 Mar, 2008 18:30:21
sprintf us 05 Mar, 2008 05:00:36
fp us 05 Mar, 2008 05:00:35
code us 05 Mar, 2008 05:00:35
str2num us 05 Mar, 2008 05:00:35
floating point us 05 Mar, 2008 05:00:35
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