We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go version
$ go version : 1.12
go env
$ go env
type A struct { ID int json:"id" Amount float64 json:"amount" }
json:"id"
json:"amount"
ok case: func main() { var a = A{ID: 1, Amount: 7.11} var b = A{ID: 2, Amount: 1.02} c := A{ID: 1, Amount: 0.0000} c.Amount = a.Amount + b.Amount fmt.Printf("%f==%+v\n", c.Amount, c) content, err := json.Marshal(c) fmt.Println(string(content), err) } output: 8.130000=={ID:1 Amount:8.13} {"id":1,"amount":8.13}
nok case :
func main() { var a = A{ID: 1, Amount: 8.11} var b = A{ID: 2, Amount: 1.02} c := A{ID: 1, Amount: 0.0000} c.Amount = a.Amount + b.Amount fmt.Printf("%f==%+v\n", c.Amount, c) content, err := json.Marshal(c) fmt.Println(string(content), err) } output: 9.130000=={ID:1 Amount:9.129999999999999} {"id":1,"amount":9.129999999999999}
9.130000=={ID:1 Amount:9.13} {"id":1,"amount":9.13}
9.130000=={ID:1 Amount:9.129999999999999} {"id":1,"amount":9.129999999999999}
the boundary is 8, can any explain is this a feature or bug,if feature, why? any advice to avoid the case or solution?
The text was updated successfully, but these errors were encountered:
Note that it is not correct to use floating point types for currency values. https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency
Sorry, something went wrong.
See https://floating-point-gui.de
This isn't really a Go issue.
can anyone explain why 8.13 is ok,but 9.13 shows 9.129999999999999, thanks a lot!
I sent you a link above with information about floats. This isn't a Go specific thing.
thanks,I will read.
No branches or pull requests
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
type A struct {
ID int
json:"id"
Amount float64
json:"amount"
}
ok case:
func main() {
var a = A{ID: 1, Amount: 7.11}
var b = A{ID: 2, Amount: 1.02}
c := A{ID: 1, Amount: 0.0000}
c.Amount = a.Amount + b.Amount
fmt.Printf("%f==%+v\n", c.Amount, c)
content, err := json.Marshal(c)
fmt.Println(string(content), err)
}
output:
8.130000=={ID:1 Amount:8.13}
{"id":1,"amount":8.13}
nok case :
func main() {
var a = A{ID: 1, Amount: 8.11}
var b = A{ID: 2, Amount: 1.02}
c := A{ID: 1, Amount: 0.0000}
c.Amount = a.Amount + b.Amount
fmt.Printf("%f==%+v\n", c.Amount, c)
content, err := json.Marshal(c)
fmt.Println(string(content), err)
}
output:
9.130000=={ID:1 Amount:9.129999999999999}
{"id":1,"amount":9.129999999999999}
What did you expect to see?
9.130000=={ID:1 Amount:9.13}
{"id":1,"amount":9.13}
What did you see instead?
9.130000=={ID:1 Amount:9.129999999999999}
{"id":1,"amount":9.129999999999999}
the boundary is 8, can any explain is this a feature or bug,if feature, why? any advice to avoid the case or solution?
The text was updated successfully, but these errors were encountered: