xxxxxxxxxx
Print only 2 digits after decimal python
xxxxxxxxxx
float = 2.154327
format_float = "{:.2f}".format(float)
print(format_float)
xxxxxxxxxx
#to round floats in Python you can use the "round" function. ex:
tax = 34.4563
tax = round(tax, 2)
#the number 2 at the end is how many digits are rounded.
#the variable "tax" would now be: 34.46
xxxxxxxxxx
# round a value to 2 decimal points
value_to_print = 123.456
print(f'value is {round(value_to_print, 2)}')
# prints: value is 123.46
xxxxxxxxxx
#include <stdio.h>
int main()
{
float num = 10.23456f;
printf("num = %.2f\n",num);
return 0;
}
output:
num = 10.23