zegna
(Zegna)
May 20, 2018, 1:28pm
1
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
NobbZ
(Norbert Melzer)
May 20, 2018, 1:49pm
2
%s
is for strings and a float isn’t a string.
Try %f
instead.
lutzhorn
(Lutz Horn)
May 20, 2018, 1:50pm
3
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
system
(system)
Closed
August 18, 2018, 1:50pm
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.