필터 지우기
필터 지우기

How do you round up or down to a decimal

조회 수: 62 (최근 30일)
Ted H
Ted H 2023년 1월 2일
편집: Ted H 2023년 1월 3일
I want to round UP to a specific decimal location (tenths in my current need).
I am getting errors using round():
a = 6.234;
b = round( a, 1);
gives 6.2. It works, but is not UP. It rounded DOWN. So I add TieBreaker:
b = round( a, 1, TieBreaker="plusinf");
gives
Error using round
Too many input arguments.
I missed something
b = round( a, TieBreaker="plusinf");
gives
Error using round
Third input must be either 'decimals' or 'significant'.
I missed something
Any comments, corrections, alternate methods are appreciated.

채택된 답변

Image Analyst
Image Analyst 2023년 1월 2일
편집: Image Analyst 2023년 1월 2일
To round up use ceil
To round down use floor
  댓글 수: 2
Ted H
Ted H 2023년 1월 3일
I don't see in ceil where you can handle the decimal. I see the time component only.
Voss
Voss 2023년 1월 3일
편집: Voss 2023년 1월 3일
You can do this kind of thing:
a = 6.234;
% round UP to the tenths:
b = ceil(a*10)/10
b = 6.3000
a = 6.237;
% round DOWN to the hundredths:
b = floor(a*100)/100
b = 6.2300

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2023년 1월 2일
편집: John D'Errico 2023년 1월 2일
You are trying to use capabilities of round that are not present in your (older) MATLAB release.
For that code to work, you need to upgrade to a current release. But a simple call to round should still work for you.
b = round(6.234,1)
b = 6.2000
c = round(6.253,1)
c = 6.3000
Just that the option you are trying to use is a more recent capability.
  댓글 수: 3
John D'Errico
John D'Errico 2023년 1월 2일
I am constantly being surprised, since I too often forget to read the release notes for every release.
Ted H
Ted H 2023년 1월 3일
편집: Ted H 2023년 1월 3일
Rereading the matlab documentation, tiebreaker is only for the exact midpoint. So there is no round up or round down. This does not solve my problem. @Image Analyst solution does work, however a minor complaint is that it reduces readability (IMO).
Unrelated to the technique, I would have thought rounding up or down to a specific decimal place was a need, but I suppose not, or it was resolved by users dividing and multiplying. Matlab was first commercialized in the 80s, and not until 2014 was this need made a function, while this is standard in many other programs.
@John D'Errico your solution is just rounding. not rounding always up or always down to a specific decimal place. I might not have made this as clear as I should have. I edited the original post.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by