我尝试从Python迁移到Golang.我目前正在研究一些数学运算,并想知道如何通过除法得到商和余值.我将在下面分享相当于Python的代码.
hours, remainder = divmod(5566, 3600)
minutes, seconds = divmod(remainder, 60)
print('%s:%s' % (minutes, seconds))
# 32:46
Run Code Online (Sandbox Code Playgroud)
以上将是我的目标.谢谢.
Kae*_*dys 19
整数除法加模数实现了这一点.
func divmod(numerator, denominator int64) (quotient, remainder int64) {
quotient = numerator / denominator // integer division, decimals are truncated
remainder = numerator % denominator
return
}
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/rimqraYE2B
编辑:定义
在整数除法的上下文中,商数是分子进入分母的整数倍.换句话说,它与十进制语句相同:FLOOR(n/d)
Modulo为您提供了这样一个部门的剩余部分.分子和分母的模数将始终在0和d-1之间(其中d是分母)
car*_*yte 10
如果你要单线,
quotient, remainder := numerator/denominator, numerator%denominator
Run Code Online (Sandbox Code Playgroud)