I have an issue in printing a float number. I tried:
a = 2
c = 4
print (str(c/a).format(1.6))
Output:
2.0
Required Output:
2.000000
How can I print up to 6 decimal places?
Use the round() function to print a float to 6 decimal places, e.g. print(round(my_float, 6)) . The round() function will round the floating-point number to 6 decimal places and will return the result. Copied!
we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%. 3f", x would have been printed rounded to 3 decimal places.
setprecision() to Specify Decimal Points We can specify the number of decimal points to print in cout by using the setprecision() function. This function is defined in the iomanip header file, which stands for input/output manipulation.
You can do: printf("%. 2lf\n", F); printf("%. 3lf\n", F); printf("%.
This can be accomplished by:
print("{:.6f}".format(your value here));
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