douquan9826 2017-05-12 20:04
浏览 122
已采纳

除以返回商和余数

I try to migrate from Python to Golang. I currently do research some math operations and wonder about how can I get both quotient and remainder value with result of a division. I'm going to share below an equivalent of Python code.

hours, remainder = divmod(5566, 3600)
minutes, seconds = divmod(remainder, 60)
print('%s:%s' % (minutes, seconds))
# 32:46

Above will be my aim. Thanks.

  • 写回答

2条回答 默认 最新

  • dousi7579 2017-05-12 20:14
    关注

    Integer division plus modulus accomplishes this.

    func divmod(numerator, denominator int64) (quotient, remainder int64) {
        quotient = numerator / denominator // integer division, decimals are truncated
        remainder = numerator % denominator
        return
    }
    

    https://play.golang.org/p/rimqraYE2B

    Edit: Definitions

    Quotient, in the context of integer division, is the number of whole times the numerator goes into the denominator. In other words, it is identical to the decimal statement: FLOOR(n/d)

    Modulo gives you the remainder of such a division. The modulus of a numerator and denominator will always be between 0 and d-1 (where d is the denominator)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题