フィルターのクリア

how can I convert a decimal to an integer?

805 ビュー (過去 30 日間)
sadel
sadel 2011 年 6 月 26 日
編集済み: Szymon Buchaniec 2020 年 5 月 14 日
how can I convert a decimal to an integer?
example
1.232422->1
2123.43242322->2123

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 6 月 26 日
  • fix to strip all the decimals
  • round can change the integer part
fix (1.9) % 1
round(1.9) % 2

その他の回答 (3 件)

Szymon Buchaniec
Szymon Buchaniec 2020 年 5 月 14 日
編集済み: Szymon Buchaniec 2020 年 5 月 14 日
All the proposed answers:
  • round(x) - round to the closest integer to x
  • floor(x) - round to the nearest integer less or equal to x
  • ceil(x) - round to the nearest integer greater than or equal to x
  • fix(x) - round to the nearest integer towards 0 (floor(x) for x >=0 and ceil(x) for x<=0)
does not convert the number, but only rounds it. Type of the number is still the same. To convert a decimal (double) number to an integer, one needs to use Conversion Functions. Rounding is automatic, but can be chosen explicitly. Examples:
  • uint16(1.8) -> 2
  • int32(floor(1.8)) -> 1

Mads Svendsen-Tune
Mads Svendsen-Tune 2011 年 6 月 26 日
There is a function: round()

Willem Dorff
Willem Dorff 2020 年 2 月 19 日
floor() to round down to nearest integer
ceil() to round up to nearest integer

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by