hey every gopher hope you will be fine
i am working on financial project and here i have float64 that stores user balance but when i subtract a balance it gives me lower value i want to fix that
code snippet
package main
import (
“fmt”
)
type Order struct {
amount,price,fee float64
As part of your coding project, I suggest you stop coding and study this issue carefully before continuing. (Especially if it is for actual use in business, rather than just a personal project or experiment.) There is no simple answer, and you will need to study and think about your options and your needs before developing code.
The best way I know of to handle financial calculations is to use a language with good support for them by doing arithmetic in decimal rather than binary. Once upon a time, that meant using COBOL, and it was one of the biggest reasons why COBOL was so popular and had such a long lifetime.
Even today, this is a difficult topic. Here are some web pages I found that may help you get started:
I, I have a application that store value as int, and as a requirement, only 4 decimal digits as valid decimal digit. So every operation is made in int value and presentation is in 2 decimal digits.
Thank you I @geosoft1 appreciate your help and your comment but i want to use floats into calculations so that this can’t be used there it can only be used to print decimal digits of a number
this is what exactly i did now i changed the float64 to unit amount as int64 now it works well but i have to divide these with total when i am saving them into db or sending values from server to clients
1.23 with a precision of 1 might get parsed as 1.19, because it’s printed as 1.2, and when parsing floats the IEEE specifies, that they have to be parsed into the next smaller number that can be represented.
If you are working with floats, there is no way around rounding issues, except not using floats!
exactly i tried this but that rounding came in to my way
so now i have gone into int64 and using unit of currencies instead of using bigger values
Thank you @NobbZ you always gave best advice