1

I have been writing a program for a project and have tried using the round() function to round a floating point variable up to the next whole number. In the case of this program, it calculates how much paint is required (in gallons) to paint a certain amount of square footage. The exercise assumes that paint is only available in whole gallons, so my calculation needs to round up all fractions of a gallon to the next whole gallons.

Any thoughts?

Thanks,

Scott

  • I tried reading through that post, but the answers were not really clear to me and I wasn't able to find a solution based on the information provided. PLEASE be assured that I did a LOT of searching before I asked the question :P – eMedic PCR - Admin Oct 11 '13 at 03:52

2 Answers2

7

Use math.ceil():

print math.ceil(3.4)
# 4.0
musical_coder
  • 3,886
  • 3
  • 15
  • 18
0

I found the error. Apparently I need to import the math module into my program. When I read the Python documentation, it stated that the math module was always available. Now that I have added "import math" to the beginning of my program, I am not getting the syntax error.

If anyone is interested in the syntax I used, here is the code:

def calculateGallonsNeeded(fltWallArea,AREA_UNIT):
    fltGallonsNeeded = math.ceil(fltWallArea / AREA_UNIT)
    return fltGallonsNeeded

Thanks for all the help.

Scott

  • 1
    For the record, not importing `math` should not have given you a `SyntaxError`. It might have given you a `NameError`, though. On another subject, what the docs mean by `math` always being available isn't that you don't have to import it, it's that it's not a platform-dependent module. – DSM Oct 11 '13 at 03:54