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

Unanticipated outcome including float numbers – Obtaining Assistance

The adhering to fragment

import "fmt".

func major() {
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 appropriately a float number with a particular variety of decimals, you require to utilize %. Nf where N is the variety of numbers you desire.
In your instance 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 instance you’re utilizing 2 float64 so Go loads all the ideal 14 numbers instantly

That’s simply drift …

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

Some history information:


1 Like

Many Thanks both, i must have understood


Most Popular

Recent Comments