Rounding towards zero or from zero

조회 수: 9 (최근 30일)
Nick Austinos
Nick Austinos 2022년 9월 26일
답변: Image Analyst 2022년 9월 26일
Hi all; I want to round 3.6 to 3 but funny enough all the 'tozero' and 'fromzero' tiebreakers are returning the same answer i.e 4 which i dont want. How can this issue be fixed?
x=3.6
y=round(x,"TieBreaker","tozero")
z=round(x,"TieBreaker","fromzero")

채택된 답변

Les Beckham
Les Beckham 2022년 9월 26일
편집: Les Beckham 2022년 9월 26일
There is no tie involved in rounding 3.6. If you wish, you can use floor instead:
floor(3.6)
ans = 3
If x was 3.5 instead, that would be a tie:
x = 3.5;
y=round(x,"TieBreaker","tozero")
y = 3
z=round(x,"TieBreaker","fromzero")
z = 4
  댓글 수: 2
Nick Austinos
Nick Austinos 2022년 9월 26일
I works thanks;
Les Beckham
Les Beckham 2022년 9월 26일
You are quite welcome.

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

추가 답변 (3개)

Sam Chak
Sam Chak 2022년 9월 26일
Does this work for you?
x = 3.6;
y = floor(x)
y = 3

Steven Lord
Steven Lord 2022년 9월 26일
The tiebreaker methods only apply when the quantity to be rounded is halfway between the two numbers to which it could be rounded. So if you had 3.5 that's halfway between 3 and 4 and the tiebreaker would determine to which of those numbers 3.5 gets rounded.
round(3.5, 'TieBreaker', 'tozero')
ans = 3
round(3.5, 'TieBreaker', 'fromzero')
ans = 4
There's no tie to be broken if you're rounding 3.6.
There are other rounding functions that you may want to use instead of round. See their help or documentation pages for more information on each function's specific behavior.
[fix(3.6), floor(3.6), round(3.6), ceil(3.6)]
ans = 1×4
3 3 4 4

Image Analyst
Image Analyst 2022년 9월 26일
Try fix it rounds towards zero regardless if it's positive or negative, unlike floor which rounds towards negative infinity.
v = fix(3.6)
v = 3
v = fix(-9.4)
v = -9
v = floor(-9.4)
v = -10

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by