当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Ruby Float.divmod用法及代码示例

本文简要介绍ruby语言中 Float.divmod 的用法。

用法

divmod(other) → array

返回一个 2 元素数组 [q, r] ,其中

q = (self/other).floor      # Quotient
r = self % other            # Remainder

例子:

11.0.divmod(4)              # => [2, 3.0]
11.0.divmod(-4)             # => [-3, -1.0]
-11.0.divmod(4)             # => [-3, 1.0]
-11.0.divmod(-4)            # => [2, -3.0]

12.0.divmod(4)              # => [3, 0.0]
12.0.divmod(-4)             # => [-3, 0.0]
-12.0.divmod(4)             # => [-3, -0.0]
-12.0.divmod(-4)            # => [3, -0.0]

13.0.divmod(4.0)            # => [3, 1.0]
13.0.divmod(Rational(4, 1)) # => [3, 1.0]

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Float.divmod。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。