The round()
function rounds off a number to a specified number of decimal places.
Figure 1 shows a visual representation of the round()
function.
num.round(ndigits)
#where num is the number which is to be rounded off
#where ndigits is the number of decimal places
The parameter ndigits
is the number of decimal places to which the above number needs to be rounded off.
This is an optional parameter. Its default value is 0. If
ndigits
is omitted, thenround()
returns the nearest integer value of a number.
The round()
function returns the number rounded off to the specified number of decimal places.
If you input a negative value (
-n
) in thendigits
, thenn
digits to the left of the decimal point of the number will be rounded off.
#number: postive ndigits: positiveprint "(9.8923).round(2):", (9.8923).round(2), "\n"#number: negative ndigits: positiveprint "(-9.8923).round(2): ", (-9.8923).round(2), "\n"#number: positive ndigits: negativeprint "(923.8923).round(-2): ", (923.8923).round(-2), "\n"print "(989.8923).round(-2): ", (989.8923).round(-2), "\n"#number: negative ndigits: negativeprint "(-923.8923).round(-2): ", (-923.8923).round(-2), "\n"print "(-989.8923).round(-2): ", (-989.8923).round(-2), "\n"# no ndigitsprint "(9.8923).round():", (9.8923).round(), "\n"print "(-9.8923).round(): ", (-9.8923).round(), "\n"