# Golang simple 1 point decimal counter

I am trying to achieve a counter that will count how many seconds have passed in my game. It’s only seconds based and I need it for a game server project. All I want to do, literally all I want to do is `0 + 0.2` and then next tick `0.2+0.2`, `0.4+0.2` … This cannot be achieved with floats as eventually it will be 1.99999999998 instead of 2 and that would ruin the calculations. I’ve looked into Big Int, fixed precision libraries but I am not sure what should I use. I don’t want too much overhead as this is called 5-10 times a second. A simple `0 + 0.2` would work perfectly and that way I can get whether or not a second or half a second has passed and I can store the previous time in a simple number.

Idea is that player would have a timestamp of when the last action was performed, I’d check against the time if X time has passed ( `time- lastTime > X`) and then if that’s true I’d perform something. I have access to the servers tick rate which I set to 0.2. I can always go with OS time if nothing else. Any recommendations on what to do?

you can use 200 milliseconds with an integer data type (int64).

Care to elaborate? I’d much prefer having smaller decimals, but I guess having bigger numbers could do the same thing

If you use 200ms as a fixed step size, you can create a custom integer that uses exactly 200ms as one step.

``````package main

import (
"fmt"
)

type Counter int64 // 200ms step
const (
Step200MS  Counter = 1
StepSecond         = 5 * Step200MS
)

func (c Counter) String() string {
return fmt.Sprintf("%d.%ds", c/5, c%5*200)
}

func main() {
var c Counter

c += Step200MS
fmt.Printf("time: %s\n", c)

c++ // equals 200ms
fmt.Printf("time: %s\n", c)

c += StepSecond
fmt.Printf("time: %s\n", c)
}
``````
1 Like

sure i could but @j-forster gave a great example

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.