2017-05-12 51 views
1

我嘗試從Python遷移到Golang。我目前正在研究一些數學運算,並想知道如何得到商數和餘數以及分數的結果。我將在下面分享一些相同的Python代碼。返回商和餘數的分區

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

以上將是我的目標。謝謝。

回答

9

整數除法模數完成此操作。

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

編輯:定義

,在整數除法的背景下,是分子進入分母的整個次數。換句話說,它是相同的十進制聲明:FLOOR(n/d)

爲您提供了這樣的分工其餘。分子和分母的模數將始終在0和d-1之間(其中d是分母)

+0

您的解決方案非常清晰。我感到困擾的是,沒有像Python那樣內置一個。 – vildhjarta