"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
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
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
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
> 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
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
"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
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
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
"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().
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.