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:23:02

Message: 1 of 10

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: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Mar, 2008 15:29:02

Message: 2 of 10

In article <fqjpgm$3hp$1@fred.mathworks.com>, Deo <spliers@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)

Subject: how to round to 2 decimal?

From: Paul

Date: 4 Mar, 2008 15:39:01

Message: 3 of 10

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqjpru$lpj$1@canopus.cc.umanitoba.ca>...
> In article <fqjpgm$3hp$1@fred.mathworks.com>, Deo
<spliers@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)


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

Subject: how to round to 2 decimal?

From: Paul

Date: 4 Mar, 2008 15:47:04

Message: 4 of 10

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fqjpru$lpj$1@canopus.cc.umanitoba.ca>...
> In article <fqjpgm$3hp$1@fred.mathworks.com>, Deo
<spliers@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)


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

Subject: how to round to 2 decimal?

From: Jerome Briot

Date: 4 Mar, 2008 17:17:02

Message: 5 of 10

"Paul " <par@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)

Subject: how to round to 2 decimal?

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

Date: 4 Mar, 2008 17:36:56

Message: 6 of 10

In article <fqjpru$lpj$1@canopus.cc.umanitoba.ca>,
Walter Roberson <roberson@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

Subject: how to round to 2 decimal?

From: chabdullahsaleem@gmail.com

Date: 5 Mar, 2013 13:33:16

Message: 7 of 10

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

Subject: how to round to 2 decimal?

From: Nasser M. Abbasi

Date: 6 Mar, 2013 05:13:48

Message: 8 of 10

On 3/5/2013 7:33 AM, chabdullahsaleem@gmail.com wrote:
> 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
>


But this does not round x to 2 decimal places.

x does not change in any way after this call.

--Nasser

Subject: how to round to 2 decimal?

From: Marco

Date: 14 Nov, 2013 21:45:14

Message: 9 of 10

"Nasser M. Abbasi" wrote in message <kh6jab$712$1@speranza.aioe.org>...
> On 3/5/2013 7:33 AM, chabdullahsaleem@gmail.com wrote:
> > 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
> >
>
>
> But this does not round x to 2 decimal places.
>
> x does not change in any way after this call.
>
> --Nasser

you may be missing the point. It is POINT2f
so
sprintf('% . 2f',x)

Subject: how to round to 2 decimal?

From: TideMan

Date: 15 Nov, 2013 01:24:38

Message: 10 of 10

On Friday, November 15, 2013 10:45:14 AM UTC+13, Marco wrote:
> "Nasser M. Abbasi" wrote in message <kh6jab$712$1@speranza.aioe.org>...
>
> > On 3/5/2013 7:33 AM, chabdullahsaleem@gmail.com wrote:
>
> > > 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
>
> > >
>
> >
>
> >
>
> > But this does not round x to 2 decimal places.
>
> >
>
> > x does not change in any way after this call.
>
> >
>
> > --Nasser
>
>
>
> you may be missing the point. It is POINT2f
>
> so
>
> sprintf('% . 2f',x)

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.

Tags for this Thread

No tags are associated with 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