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:
Rounding to significant figures

Subject: Rounding to significant figures

From: JP

Date: 30 Mar, 2011 00:24:04

Message: 1 of 5

Hi all,

I need help with rounding down a value to one significant figure.

if x = 0.0047;

how do i round it down to just x = 0.004; is this possible?

if i use round it will bring x = 0.
if i use ceil it will bring it to 1.
floor will be x = 0 also.
num2str(x,1) will bring it 0.005.

I'm basically out of functions, please help. and thanks in advance.


JP

Subject: Rounding to significant figures

From: dpb

Date: 30 Mar, 2011 01:00:51

Message: 2 of 5

On 3/29/2011 7:24 PM, JP wrote:
> Hi all,
>
> I need help with rounding down a value to one significant figure.
>
> if x = 0.0047;
>
> how do i round it down to just x = 0.004; is this possible?
>
> if i use round it will bring x = 0.
> if i use ceil it will bring it to 1.
> floor will be x = 0 also.
> num2str(x,1) will bring it 0.005.
>
> I'm basically out of functions, please help. and thanks in advance.

If you must truncate instead of round you're at the point of needing to
do it yourself...

 >> x=0.0047;
 >> fix(x/10^floor(log10(x)))*10^floor(log10(x)) % salt to suit...
ans =
     0.0040
 >>

Alternatively, you can write and parse a formatted string but have to be
sure have sufficient precision there as well or the i/o runtime will
round for you if not careful, too.

--

Subject: Rounding to significant figures

From: JP

Date: 30 Mar, 2011 01:20:07

Message: 3 of 5

> > I'm basically out of functions, please help. and thanks in advance.
>
> If you must truncate instead of round you're at the point of needing to
> do it yourself...
>
> >> x=0.0047;
> >> fix(x/10^floor(log10(x)))*10^floor(log10(x)) % salt to suit...
> ans =
> 0.0040
> >>
>
> Alternatively, you can write and parse a formatted string but have to be
> sure have sufficient precision there as well or the i/o runtime will
> round for you if not careful, too.
>
> --

works perfectly. thank you.

if however, i want to round it up so that x = 0.0043 will equal to x = 0.005.

JP

Subject: Rounding to significant figures

From: JP

Date: 30 Mar, 2011 01:29:05

Message: 4 of 5

replace the fix function with ceil... thanks so much.

JP

Subject: Rounding to significant figures

From: ImageAnalyst

Date: 30 Mar, 2011 01:30:54

Message: 5 of 5

That's where you were supposed to add salt. One simpler, though less
general purpose way is this:

xr = ceil(x*1000)/1000

which is similar to this salty method:
xr = ceil(x/10^floor(log10(x)))*10^floor(log10(x))

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