Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to round to 2 decimal?

Subject: how to round to 2 decimal?

From: Deo

Date: 4 Mar, 2008 15:26:02

Message: 1 of 12

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

Subject: how to round to 2 decimal?

From: Dan Haeg

Date: 4 Mar, 2008 23:51:03

Message: 2 of 12

"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: how to round to 2 decimal?

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

Date: 5 Mar, 2008 00:26:27

Message: 3 of 12

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: how to round to 2 decimal?

From: nor ki

Date: 5 Mar, 2008 08:28:01

Message: 4 of 12

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: how to round to 2 decimal?

From: us

Date: 5 Mar, 2008 09:58:02

Message: 5 of 12

"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: how to round to 2 decimal?

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

Date: 5 Mar, 2008 17:36:28

Message: 6 of 12

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: how to round to 2 decimal?

From: carlos lopez

Date: 5 Mar, 2008 23:30:18

Message: 7 of 12

"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: how to round to 2 decimal?

From: Omar U. Florez

Date: 28 Jun, 2008 22:09:01

Message: 8 of 12

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: how to round to 2 decimal?

From: zia

Date: 28 Jun, 2008 22:18:35

Message: 9 of 12

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: how to round to 2 decimal?

From: us

Date: 28 Jun, 2008 22:45:04

Message: 10 of 12

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: how to round to 2 decimal?

From: Nasser Abbasi

Date: 29 Jun, 2008 09:21:55

Message: 11 of 12


"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

Subject: how to round to 2 decimal?

From: D. Weyer

Date: 12 May, 2011 11:02:05

Message: 12 of 12

Ich chose sprintf('%.3g', pi)

Tags for this Thread

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.

Contact us