Skip to content

Instantly share code, notes, and snippets.

@xerardoo
Last active June 9, 2022 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xerardoo/ad06fc53102df6cb011f7516ac8efa32 to your computer and use it in GitHub Desktop.
Save xerardoo/ad06fc53102df6cb011f7516ac8efa32 to your computer and use it in GitHub Desktop.
How manage money in go
// https://golangprojectstructure.com/representing-money-and-currency-go-code/
// Manage in cents (integer)
package main
import "fmt"
func main() {
var sum float32
for i := 0; i < 1000000; i++ {
sum += float32(0.1) * 1.55
}
var expectedSum int
for i := 0; i < 100000; i++ {
expectedSum += 1 * 155
}
fmt.Printf("$ %f\n", sum) // $ 155919.968750
fmt.Println("$", expectedSum / 100) // $ 155000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment