Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to round to 2 decimal?

5,287 views
Skip to first unread message

Deo

unread,
Mar 4, 2008, 10:23:02 AM3/4/08
to
hey, how can I set my matlab program to round all the
double numbers to 2 decimals

Deo

unread,
Mar 4, 2008, 10:26:02 AM3/4/08
to

Walter Roberson

unread,
Mar 4, 2008, 10:29:02 AM3/4/08
to
In article <fqjpgm$3hp$1...@fred.mathworks.com>, Deo <spl...@hotmail.com> wrote:
>hey, how can I set my matlab program to round all the
>double numbers to 2 decimals

You cannot. There is no exact representation in floating point
for .01, .02, up to .99, except for 0.25, 0.50, and 0.75 which
have exact representations. If you were to try to round
(say) pi to 2 decimal places then the value stored internally
would not correspond to 3.14 but instead to approximately
3.14 + 2e-15

If you are only attempting to change what is displayed when
you specifically ask to output something, then you can
format the numbers using num2str or sprintf() using a format
such as '%0.2f' .
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)

Paul

unread,
Mar 4, 2008, 10:39:01 AM3/4/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqjpru$lpj$1...@canopus.cc.umanitoba.ca>...


x = round((x*100))/100

Paul

unread,
Mar 4, 2008, 10:47:04 AM3/4/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqjpru$lpj$1...@canopus.cc.umanitoba.ca>...


x = round((x*100))/100

Jerome Briot

unread,
Mar 4, 2008, 12:17:02 PM3/4/08
to
"Paul " <p...@ceri.memphis.edu> wrote in message
> x = round((x*100))/100

Hi,

>> x=pi;
x = round((x*100))/100

sprintf('%.16f',x)

x =

3.1400


ans =

3.1400000000000001

Jerome (Dut)

Walter Roberson

unread,
Mar 4, 2008, 12:36:56 PM3/4/08
to
In article <fqjpru$lpj$1...@canopus.cc.umanitoba.ca>,

Walter Roberson <robe...@ibd.nrc-cnrc.gc.ca> wrote:
>If you were to try to round
>(say) pi to 2 decimal places then the value stored internally
>would not correspond to 3.14 but instead to approximately
>3.14 + 2e-15

Small correction: the result is
3.14 + 1.24344978758017532527446746826171875e-16

>> sprintf('%0.60g',3.14)

ans =

3.140000000000000124344978758017532527446746826171875


--
"All human knowledge takes the form of interpretation."
-- Walter Benjamin

Dan Haeg

unread,
Mar 4, 2008, 6:51:03 PM3/4/08
to
"Deo " <spl...@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.

Walter Roberson

unread,
Mar 4, 2008, 7:26:27 PM3/4/08
to
In article <fqkn97$hnn$1...@fred.mathworks.com>, Dan Haeg <ha...@msoe.edu> wrote:
>"Deo " <spl...@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???

nor ki

unread,
Mar 5, 2008, 3:28:01 AM3/5/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqkpbj$72h$1...@canopus.cc.umanitoba.ca>...

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

us

unread,
Mar 5, 2008, 4:58:02 AM3/5/08
to
"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

Walter Roberson

unread,
Mar 5, 2008, 12:36:28 PM3/5/08
to
In article <fqllih$doa$1...@fred.mathworks.com>,

nor ki <kinor.r...@gmx.de> wrote:
>robe...@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
><ha...@msoe.edu> wrote:
>> >"Deo " <spl...@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

carlos lopez

unread,
Mar 5, 2008, 6:30:18 PM3/5/08
to
"Deo " <spl...@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

Omar U. Florez

unread,
Jun 28, 2008, 6:09:01 PM6/28/08
to
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_...@adinet.com.uy> wrote in
message <fqnaea$370$1...@fred.mathworks.com>...

zia

unread,
Jun 28, 2008, 6:18:35 PM6/28/08
to
On Jun 28, 6:09 pm, "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 =
> 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>...


Hello,

Try this

x = 1.23456

quant(x,2)


function output = quant(number,digit)

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


--Zia

us

unread,
Jun 28, 2008, 6:45:04 PM6/28/08
to
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

Nasser Abbasi

unread,
Jun 29, 2008, 5:21:55 AM6/29/08
to

"us " <u...@neurol.unizh.ch> wrote in message
news:g46etg$pmu$1...@fred.mathworks.com...

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

D. Weyer

unread,
May 12, 2011, 7:02:05 AM5/12/11
to
Ich chose sprintf('%.3g', pi)

chabdull...@gmail.com

unread,
Mar 5, 2013, 8:33:16 AM3/5/13
to Deo
On Tuesday, March 4, 2008 8:23:02 PM UTC+5, Deo wrote:
> hey, how can I set my matlab program to round all the
> double numbers to 2 decimals
LET TAKE A SIMPLE EXAMPLE
x=pi;
sprintf('%.2f',x)

now type this code in your m file and run it your output will be:

x

3.14

Nasser M. Abbasi

unread,
Mar 6, 2013, 12:13:48 AM3/6/13
to
But this does not round x to 2 decimal places.

x does not change in any way after this call.

--Nasser

Marco

unread,
Nov 14, 2013, 4:45:14 PM11/14/13
to
"Nasser M. Abbasi" wrote in message <kh6jab$712$1...@speranza.aioe.org>...
you may be missing the point. It is POINT2f
so
sprintf('% . 2f',x)

TideMan

unread,
Nov 14, 2013, 8:24:38 PM11/14/13
to
No, Marco, it is YOU who are missing the point.
Nasser is correct.
After:
sprintf('%.2f',x)
you get 3.14, but x has not changed.

yan mercal

unread,
Jun 9, 2015, 3:26:35 AM6/9/15
to
float f = 10.123456F;
float fc = (float)Math.Round(f * 100f) / 100f;

More about.....

http://net-informations.com/q/faq/round.html

Mercal

"Deo " <spl...@hotmail.com> wrote in message <fqjpgm$3hp$1...@fred.mathworks.com>...

Steven Lord

unread,
Jun 9, 2015, 9:28:17 AM6/9/15
to


"yan mercal" <yanm...@yahoo.com> wrote in message
news:ml64f6$d6t$1...@newscl01ah.mathworks.com...
> float f = 10.123456F;
> float fc = (float)Math.Round(f * 100f) / 100f;

*snip*

The message to which you replied was seven years old and had already
received a number of responses. In addition, your answer appears to be Java
code. While you can call Java from MATLAB, in this case if you're using
release R2014b or later you can just use ROUND. See item 4 in the
Mathematics section of the Release Notes for release R2014b on this page:

http://www.mathworks.com/help/matlab/release-notes.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Chaman Lal Dewangan

unread,
Oct 18, 2015, 7:19:13 AM10/18/15
to
"Godzilla" wrote in message <fqjqel$hok$1...@fred.mathworks.com>...
Yes It works
Thank You
0 new messages