Tuesday, March 14, 2023
HomeGolangUnanticipated outcome including float numbers - Obtaining Assistance

Unanticipated outcome including float numbers – Obtaining Assistance


The adhering to bit


import "fmt".

func primary() {
a:= 8.9.
b:= 2.2.
c:= a + b.
fmt.Printf("% gn", c).
}

Returns 11.100000000000001 yet i anticipate 11.1

What am i doing incorrect?

Many Thanks


Hi @wdantuma :slight_smile:! In Go if you to desire layout properly a float number with a particular variety of decimals, you require to utilize %. Nf where N is the variety of figures you desire.
In your instance modification:

with fmt.Printf("%.1 fn", c) The formatter % g stands for the float number with its optimum variety of considerable figures, as well as in your instance you’re utilizing 2 float64 so Go fills up all the appropriate 14 figures instantly

That’s simply drift …

If you require exact outcomes, simply do not utilize drifts.

Some history details:

https://0.30000000000000004.com/



1 Like

Many Thanks both, i ought to have recognized

RELATED ARTICLES

Most Popular

Recent Comments