Matlab equivalent to Java bigdecimal rounding of Half_up?

3 ビュー (過去 30 日間)
Derek
Derek 2024 年 5 月 31 日
コメント済み: Derek 2024 年 6 月 4 日
Is there a Matlab function that mimics the Half_up rounding in Java bigdecimal? Specifically, when the decimal portion is exactly .5, round is towards positive infinity. Example: 3.5 rounds to 4 and -3.5 rounds to -3. I didn't see anything like this in the help for round().

採用された回答

Steven Lord
Steven Lord 2024 年 5 月 31 日
In release R2022a we added the argument TieBreaker (which can be used case insensitively) to control how ties are broken in round. Are you using an older release?
x = [3.5, -3.5];
round(x, Tiebreaker = "plusinf")
ans = 1x2
4 -3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
round(x, Tiebreaker = "tozero")
ans = 1x2
3 -3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
round(x, Tiebreaker = "fromzero") % default
ans = 1x2
4 -4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can use this with the N input argument too.
round(1.25, 1) % Remember "fromzero" is the default
ans = 1.3000
round(1.25, 1, Tiebreaker = "tozero")
ans = 1.2000
  1 件のコメント
Derek
Derek 2024 年 6 月 4 日
Thank you - yes, I am on an R2020b so I guess I need to upgrade if I want this function.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by