Martin Møller Skarbiniks Pedersen,
Use a RoundUp function for n / d. For example,
// n / d rounded up
func RoundUp(n, d int64) int64 {
if d == 0 {
return 0
}
return (n + (d - 1)) / d
}
https://play.golang.org/p/kEMJ04ggkMc
Peter
On Tuesday, August 4, 2020 at 6:37:06 AM UTC-4, Martin Møller Skarbiniks
Pedersen wrote:
>
> On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
>> [email protected]> wrote:
>>
>>> I have written my first little piece of Go-code.
>>> However it took some time and code for me to divide a int64 by 4 and
>>> round down.
>>>
>>> [...]
>>> var bet int64
>>> bet = int64(math.Ceil(float64(cash)/float64(4)))
>>>
>>>
> On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
>>
>> In addition to Jake's question regarding whether you want to round up or
>> down I'll point out there isn't any to cast to float64 and back to int64.
>> If you want to "round down" just divide by four. If you want to "round up"
>> add one less than the divisor before dividing; e.g., bet := (cash + 3) /
>> 4. Notice the ":=" which avoids the need for the "var bet int64" statement.
>>
>
>
> OK. Good idea. But what is I need to integer divide with a variable and
> not the number 4?
>
> Cheers
> Martin
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/47ff7c07-4974-4a33-87e5-73bcbc9409e9o%40googlegroups.com.