Last active
June 9, 2022 11:00
-
-
Save xerardoo/ad06fc53102df6cb011f7516ac8efa32 to your computer and use it in GitHub Desktop.
How manage money in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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