MATLAB Answers

huda nawaf

how rounde no. if third digit after floating more than 5

Asked by huda nawaf
on 9 Apr 2013

hi, I want raounded a number, so I read this link about that But in this case , it rounde any no. ,while I want rounde number meet a condition. for example:

0.2371 be 0.2400


0.4546 be 0.4600

but this number 0.2330 , must be not equal 0.2400, must be the same i.e I want round number if the third digit after floating point equal or more than 5

Thanks in advance


Then why would 0.4546 round to 0.4600 and not to .4500, considering that the third digit is not equal or more than 5 ?

Are you wanting to do this numerically or for display purposes?

thanks, I supposed 0.4546 is equal 0.4550.

currently, I want it just for display.



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 10 Apr 2013
 Accepted answer

sprintf('%.2f00', round(N * 100) / 100)

Though I admit to being puzzled as to why you want the misleading '00' at the end, why you want 0.2400 instead of 0.24

  1 Comment

no , I mean 0.24

thanks, sprints works

Answer by Jan Simon
on 10 Apr 2013

If you want it just for displaying, what about:

sprintf('%.3f\n', [0.2371, 0.4546])

I'd definitly omit the trilaing zero, because it is not only confusing, but wrong.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!