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:
Round down to nearest decimal???

Subject: Round down to nearest decimal???

From: JP

Date: 30 Mar, 2011 00:09:04

Message: 1 of 4

Hi all,

I need help with the rounding functions in MATLAB.

For example if I have a value
         x = 0.0047;

and I'd like to round it down to one significant figure. would I be able to do that?

i.e. round x so that it equals to x = 0.004;

Please help. Thanks in advance.


JP

Subject: Round down to nearest decimal???

From: Roger Stafford

Date: 30 Mar, 2011 01:02:04

Message: 2 of 4

"JP" wrote in message <imtsb0$lgp$1@fred.mathworks.com>...
> Hi all,
>
> I need help with the rounding functions in MATLAB.
>
> For example if I have a value
> x = 0.0047;
>
> and I'd like to round it down to one significant figure. would I be able to do that?
>
> i.e. round x so that it equals to x = 0.004;
>
> Please help. Thanks in advance.
>
>
> JP
- - - - - - - -
 y = 10^floor(log10(x));
 y = round(x/y)*y;

Roger Stafford

Subject: Round down to nearest decimal???

From: JP

Date: 30 Mar, 2011 01:13:04

Message: 3 of 4

> - - - - - - - -
> y = 10^floor(log10(x));
> y = round(x/y)*y;
>
> Roger Stafford

Hi,

i have tried this and this seems to just round it to one significant figure. similar to using num2str

JP

Subject: Round down to nearest decimal???

From: Roger Stafford

Date: 30 Mar, 2011 01:37:04

Message: 4 of 4

"JP" wrote in message <imu030$jjt$1@fred.mathworks.com>...
> i have tried this and this seems to just round it to one significant figure. similar to using num2str
> JP
- - - - - - - - - -
 Dpb in the other thread interpreted your 'down' statement correctly with the 'fix' function. To handle negative numbers you need:

 y = 10^floor(log10(abs(x)));
 y = fix(x/y)*y;

Roger Stafford

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