I have the following code snippet:
package main
import("fmt";"flag")
func main() {
var a = flag.Int("a",0,"divident")
var b = flag.Int("b",1,"divisor")
flag.Parse()
fmt.Printf("%f",*a / *b )
}
For -a 3 and -b 2 command line arguments, the output is: %!f(int=1)
What is the best / most elegant way to force this division to be floating point?
To divide float values in Python, use the / operator. The Division operator / takes two parameters and returns the float division. Float division produces a floating-point conjecture of the result of a division. If you are working with Python 3 and you need to perform a float division, then use the division operator.
In Python, the “//” operator works as a floor division for integer and float arguments. However, the division operator '/' returns always a float value.
Golang Division Operator takes two operands and returns the division of first operand by second operand. Both the operands provided to Division Operator should be of same datatype. Golang Division Operator applies to integers, floats, and complex values. Division Operator is also called Quotient Operator.
In Python 3, "/" uniformly works as a float division operator. So, it always returns the float type: 10/3 returns 3.333333 instead of 3, 6/3 returns 2.0 instead of 2.
There are no implicit type casts for variables in Go, so you must convert to float:
fmt.Printf("%f", float32(a)/float32(b))
or
fmt.Printf("%f", float32(a/b))
Depending upon what you want. Also check out float64
-- if that floats your boat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With