我猜使用模运算符 (%%) 和 1 作为除数。它确实成功地隔离了小数后面的数字,但保留了小数:
123.456 %% 1
[1] 0.456
Run Code Online (Sandbox Code Playgroud)
然而,它仅适用于正数,因为不保留符号,也不报告正确的数字(模运算符功能的 b/c):
-123.456 %% 1
[1] 0.544 #not what is wanted
Run Code Online (Sandbox Code Playgroud)
包括abs()修复该问题,但无助于报告签名。可以通过添加以下内容来包含符号:
sign(x) * (abs(x) %% 1)
Run Code Online (Sandbox Code Playgroud)