Frustrated
Frustrated

Reputation: 131

Divide 2 big.Floats in go and preserve the resulting decimal

I have 2 big.Float numbers in go.

I want to find out what percent of x y is. This should be around 0.0020967578%. The issue comes when dividing these 2 big floats the answer is always 0.xxx but the method returns 2.09675776180520477879426e-05. Any ideas to fix this? I have tried converting to a string then back but that isn't another rabbit hole I won't include because I wasn't able to accomplish anything with it. I feel like there is a method to do this I am missing. I really only need 7 decimals of precision of that helps.

Upvotes: 0

Views: 474

Answers (1)

Zombo
Zombo

Reputation: 1

Why do you need big.Float? Even float32 seem to be fine:

package main
import "fmt"

func percent(y, x float32) float32 {
   return y / x * 100
}

func main() {
   p := percent(1.954478300965909786, 93214.310998100256907925)
   fmt.Println(p) // 0.0020967575
}

Upvotes: 1

Related Questions