How can I get my program to print floats?

I've written a small program to help me with my math homework:

import math
def saCircle():
    while True: 
        radius = float(raw_input("Enter the radius: "))
        print "\nFinding area with %d as the radius" % radius  
        x = math.pi * radius**2
        print "\nThe area of your circle is %d\n" % x 
saCircle() 

The problem is that it will accept a decimal number but it will not print out the value of the decimal number.

How can I fix this?

Answers


Use %f instead of %d (rounds your number to an integer) to print a float:

>>> radius = 4.4
>>> x = math.pi * radius**2
>>> print "\nThe area of your circle is %f\n" % x

The area of your circle is 60.821234

This question explains the difference very well.


The format specifier for floats is %f, not %d (%d is for integers).

print "\nFinding area with %f as the radius" % radius
                           ^ 

See the Wikipedia article on printf format strings for more details.


Need Your Help

SQL SELECT with an exception in a WCF service

sql wcf windows-10-universal

I have a method in the WCF service with a SQL query for a basic log in authentication :