Saturday, March 11, 2023
HomeGolangUnforeseen outcome including float numbers - Obtaining Assistance

Unforeseen outcome including float numbers – Obtaining Assistance


The complying with fragment


import "fmt".

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

Returns 11.100000000000001 however i anticipate 11.1

What am i doing incorrect?

Many Thanks


Hi @wdantuma :slight_smile:! In Go if you to desire style appropriately a float number with a details variety of decimals, you require to make use of %. Nf where N is the variety of numbers you desire.
In your situation adjustment:

with fmt.Printf("%.1 fn", c) The formatter % g stands for the float number with its optimum variety of substantial numbers, as well as in your situation you’re making use of 2 float64 so Go loads all the appropriate 14 numbers instantly

That’s simply drift …

If you require exact outcomes, simply do not make use of drifts.

Some history details:

https://0.30000000000000004.com/



1 Like

Many Thanks both, i ought to have understood

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments