""" Purpose: demonstrate use of round() function to get a number with desired number of significant digits after the decimal point. """ # get input and convert to decimal reply = input( "Enter number: " ) number = float( reply ) # takes input and converts it to a decimal print() # compute eight versions of number number6 = round( number, 6 ) # rounds to six places after the decimal number5 = round( number, 5 ) # rounds to five places after the decimal number4 = round( number, 4 ) # rounds to four places after the decimal number3 = round( number, 3 ) # rounds to three places after the decimal number2 = round( number, 2 ) # rounds to two places after the decimal number1 = round( number, 1 ) # rounds to one place after the decimal number0 = round( number, 0 ) # rounds to zero places after the decimal # (produces a decimal) rounded = round( number ) # rounds to an integer # if no number of digits is given, python will give you back an integer # round() function only recognizes places after the decimal # display original number and its eight variants print( "number:", number ) print() print( "round( number, 6 ):", number6 ) print( "round( number, 5 ):", number5 ) print( "round( number, 4 ):", number4 ) print( "round( number, 3 ):", number3 ) print( "round( number, 2 ):", number2 ) print( "round( number, 1 ):", number1 ) print( "round( number, 0 ):", number0 ) print() print( "round( number ):", rounded ) # round is a built-in function, not a method (like split() ) # round( number, places ) is valid # x = number.round() is nothing # methods v built-in functions # methods are a behavior of an object that the objects (like strings) have been designed to be able to do # there is a format() function that helps us make sure that everything has the right number of zeroes # we care more about problem solving than making the output pretty and perfect