Calculating percentage on a float

How do I calculate a percentage on a float and print out the result. Here i’m trying to calculate 0.00009037 with an increase of 5%. After starring at the docs and some blogs for some hours, I am lost.

	myfloat := float64(0.00009037)
	fmt.Printf("%s", myfloat * 1.05)

output:
%!s(float64=9.488850000000001e-05

%s is for strings and a float isn’t a string.

Try %f instead.

There is nothing wrong with your code. Do you know the scientific notation? That’s what the e-05 is about.

Let’s print your floats in a different way:

package main

import (
	"fmt"
)

func main() {
	myfloat := float64(0.00009037)
	fmt.Printf("%.10f\n", myfloat)
	fmt.Printf("%.10f\n", myfloat*1.05)
}

Here we print the floats using %f with 10 digits after the decimal point. The result:

0.0000903700
0.0000948885
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.