how can I convert a decimal to an integer?
显示 更早的评论
how can I convert a decimal to an integer?
example
1.232422->1
2123.43242322->2123
采纳的回答
更多回答(3 个)
Szymon Buchaniec
2020-5-14
编辑:Szymon Buchaniec
2020-5-14
6 个投票
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
2011-6-26
2 个投票
There is a function: round()
Willem Dorff
2020-2-19
0 个投票
floor() to round down to nearest integer
ceil() to round up to nearest integer
类别
在 帮助中心 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!