[{"data":1,"prerenderedAt":1586},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-round-function-explained":3},{"id":4,"title":5,"body":6,"description":1579,"extension":1580,"meta":1581,"navigation":579,"path":1582,"seo":1583,"stem":1584,"__hash__":1585},"content\u002Freference\u002Fpython-round-function-explained.md","Python round() Function Explained",{"type":7,"value":8,"toc":1545},"minimark",[9,19,26,29,45,48,53,108,111,126,135,142,158,161,165,168,192,210,213,277,279,299,308,315,318,353,355,370,373,425,427,460,470,476,479,521,523,537,540,604,606,621,639,645,651,718,720,739,742,762,765,769,775,778,813,815,829,840,843,906,908,929,932,935,953,959,963,968,975,1033,1035,1053,1056,1101,1103,1111,1117,1122,1136,1138,1172,1174,1183,1187,1192,1203,1206,1238,1240,1252,1255,1259,1264,1305,1307,1315,1318,1341,1345,1348,1358,1361,1421,1423,1432,1436,1441,1452,1456,1467,1470,1476,1479,1486,1496,1507,1514,1518,1541],[10,11,13,14,18],"h1",{"id":12},"python-round-function-explained","Python ",[15,16,17],"code",{},"round()"," Function Explained",[20,21,22,23,25],"p",{},"The built-in ",[15,24,17],{}," function returns a rounded version of a number.",[20,27,28],{},"You can use it in two common ways:",[30,31,32,39],"ul",{},[33,34,35,38],"li",{},[15,36,37],{},"round(number)"," rounds to the nearest whole number",[33,40,41,44],{},[15,42,43],{},"round(number, digits)"," rounds to a chosen number of decimal places",[20,46,47],{},"This is useful when you want cleaner numeric output or when you need to limit decimal places in a result.",[49,50,52],"h2",{"id":51},"quick-example","Quick example",[54,55,60],"pre",{"className":56,"code":57,"language":58,"meta":59,"style":59},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","print(round(3.14159))\nprint(round(3.14159, 2))\n","python","",[15,61,62,87],{"__ignoreMap":59},[63,64,67,71,75,78,80,84],"span",{"class":65,"line":66},"line",1,[63,68,70],{"class":69},"sptTA","print",[63,72,74],{"class":73},"sP7_E","(",[63,76,77],{"class":69},"round",[63,79,74],{"class":73},[63,81,83],{"class":82},"srdBf","3.14159",[63,85,86],{"class":73},"))\n",[63,88,90,92,94,96,98,100,103,106],{"class":65,"line":89},2,[63,91,70],{"class":69},[63,93,74],{"class":73},[63,95,77],{"class":69},[63,97,74],{"class":73},[63,99,83],{"class":82},[63,101,102],{"class":73},",",[63,104,105],{"class":82}," 2",[63,107,86],{"class":73},[20,109,110],{},"Output:",[54,112,114],{"className":56,"code":113,"language":58,"meta":59,"style":59},"3\n3.14\n",[15,115,116,121],{"__ignoreMap":59},[63,117,118],{"class":65,"line":66},[63,119,120],{"class":82},"3\n",[63,122,123],{"class":65,"line":89},[63,124,125],{"class":82},"3.14\n",[20,127,128,129,131,132,134],{},"Use ",[15,130,37],{}," to round to the nearest whole number, or ",[15,133,43],{}," to keep a set number of decimal places.",[49,136,138,139,141],{"id":137},"what-round-does","What ",[15,140,17],{}," does",[30,143,144,149,152,155],{},[33,145,146,148],{},[15,147,17],{}," is a built-in Python function",[33,150,151],{},"It returns a rounded version of a number",[33,153,154],{},"With one argument, it rounds to the nearest integer",[33,156,157],{},"With two arguments, it rounds to a chosen number of decimal places",[20,159,160],{},"You do not need to import anything to use it.",[49,162,164],{"id":163},"basic-syntax","Basic syntax",[20,166,167],{},"The syntax is:",[54,169,171],{"className":56,"code":170,"language":58,"meta":59,"style":59},"round(number[, ndigits])\n",[15,172,173],{"__ignoreMap":59},[63,174,175,177,179,183,186,189],{"class":65,"line":66},[63,176,77],{"class":69},[63,178,74],{"class":73},[63,180,182],{"class":181},"slqww","number",[63,184,185],{"class":73},"[,",[63,187,188],{"class":181}," ndigits",[63,190,191],{"class":73},"])\n",[30,193,194,199,205],{},[33,195,196,198],{},[15,197,182],{}," is the value to round",[33,200,201,204],{},[15,202,203],{},"ndigits"," is optional",[33,206,207,209],{},[15,208,203],{}," can be positive, zero, or negative",[20,211,212],{},"Basic examples:",[54,214,216],{"className":56,"code":215,"language":58,"meta":59,"style":59},"print(round(7.8))\nprint(round(7.8, 1))\nprint(round(1234, -2))\n",[15,217,218,233,252],{"__ignoreMap":59},[63,219,220,222,224,226,228,231],{"class":65,"line":66},[63,221,70],{"class":69},[63,223,74],{"class":73},[63,225,77],{"class":69},[63,227,74],{"class":73},[63,229,230],{"class":82},"7.8",[63,232,86],{"class":73},[63,234,235,237,239,241,243,245,247,250],{"class":65,"line":89},[63,236,70],{"class":69},[63,238,74],{"class":73},[63,240,77],{"class":69},[63,242,74],{"class":73},[63,244,230],{"class":82},[63,246,102],{"class":73},[63,248,249],{"class":82}," 1",[63,251,86],{"class":73},[63,253,255,257,259,261,263,266,268,272,275],{"class":65,"line":254},3,[63,256,70],{"class":69},[63,258,74],{"class":73},[63,260,77],{"class":69},[63,262,74],{"class":73},[63,264,265],{"class":82},"1234",[63,267,102],{"class":73},[63,269,271],{"class":270},"smGrS"," -",[63,273,274],{"class":82},"2",[63,276,86],{"class":73},[20,278,110],{},[54,280,282],{"className":56,"code":281,"language":58,"meta":59,"style":59},"8\n7.8\n1200\n",[15,283,284,289,294],{"__ignoreMap":59},[63,285,286],{"class":65,"line":66},[63,287,288],{"class":82},"8\n",[63,290,291],{"class":65,"line":89},[63,292,293],{"class":82},"7.8\n",[63,295,296],{"class":65,"line":254},[63,297,298],{"class":82},"1200\n",[20,300,301,302,307],{},"If you are still getting used to Python number types, see ",[303,304,306],"a",{"href":305},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int, float, complex",".",[49,309,311,312,314],{"id":310},"using-round-with-one-argument","Using ",[15,313,17],{}," with one argument",[20,316,317],{},"When you pass only one argument, Python rounds to the nearest whole number.",[54,319,321],{"className":56,"code":320,"language":58,"meta":59,"style":59},"print(round(4.2))\nprint(round(4.8))\n",[15,322,323,338],{"__ignoreMap":59},[63,324,325,327,329,331,333,336],{"class":65,"line":66},[63,326,70],{"class":69},[63,328,74],{"class":73},[63,330,77],{"class":69},[63,332,74],{"class":73},[63,334,335],{"class":82},"4.2",[63,337,86],{"class":73},[63,339,340,342,344,346,348,351],{"class":65,"line":89},[63,341,70],{"class":69},[63,343,74],{"class":73},[63,345,77],{"class":69},[63,347,74],{"class":73},[63,349,350],{"class":82},"4.8",[63,352,86],{"class":73},[20,354,110],{},[54,356,358],{"className":56,"code":357,"language":58,"meta":59,"style":59},"4\n5\n",[15,359,360,365],{"__ignoreMap":59},[63,361,362],{"class":65,"line":66},[63,363,364],{"class":82},"4\n",[63,366,367],{"class":65,"line":89},[63,368,369],{"class":82},"5\n",[20,371,372],{},"With a normal float, the result is usually an integer:",[54,374,376],{"className":56,"code":375,"language":58,"meta":59,"style":59},"result = round(10.2)\nprint(result)\nprint(type(result))\n",[15,377,378,398,409],{"__ignoreMap":59},[63,379,380,384,387,390,392,395],{"class":65,"line":66},[63,381,383],{"class":382},"su5hD","result ",[63,385,386],{"class":270},"=",[63,388,389],{"class":69}," round",[63,391,74],{"class":73},[63,393,394],{"class":82},"10.2",[63,396,397],{"class":73},")\n",[63,399,400,402,404,407],{"class":65,"line":89},[63,401,70],{"class":69},[63,403,74],{"class":73},[63,405,406],{"class":181},"result",[63,408,397],{"class":73},[63,410,411,413,415,419,421,423],{"class":65,"line":254},[63,412,70],{"class":69},[63,414,74],{"class":73},[63,416,418],{"class":417},"sZMiF","type",[63,420,74],{"class":73},[63,422,406],{"class":181},[63,424,86],{"class":73},[20,426,110],{},[54,428,430],{"className":56,"code":429,"language":58,"meta":59,"style":59},"10\n\u003Cclass 'int'>\n",[15,431,432,437],{"__ignoreMap":59},[63,433,434],{"class":65,"line":66},[63,435,436],{"class":82},"10\n",[63,438,439,442,446,450,454,457],{"class":65,"line":89},[63,440,441],{"class":270},"\u003C",[63,443,445],{"class":444},"sbsja","class",[63,447,449],{"class":448},"sjJ54"," '",[63,451,453],{"class":452},"s_sjI","int",[63,455,456],{"class":448},"'",[63,458,459],{"class":270},">\n",[20,461,462,463,307],{},"If you need to convert values directly to integers without rounding rules, compare this with ",[303,464,13,466,469],{"href":465},"\u002Freference\u002Fpython-int-function-explained\u002F",[15,467,468],{},"int()"," function explained",[49,471,311,473,475],{"id":472},"using-round-with-decimal-places",[15,474,17],{}," with decimal places",[20,477,478],{},"You can pass a second argument to control how many decimal places to keep.",[54,480,482],{"className":56,"code":481,"language":58,"meta":59,"style":59},"print(round(3.14159, 2))\nprint(round(3.14159, 3))\n",[15,483,484,502],{"__ignoreMap":59},[63,485,486,488,490,492,494,496,498,500],{"class":65,"line":66},[63,487,70],{"class":69},[63,489,74],{"class":73},[63,491,77],{"class":69},[63,493,74],{"class":73},[63,495,83],{"class":82},[63,497,102],{"class":73},[63,499,105],{"class":82},[63,501,86],{"class":73},[63,503,504,506,508,510,512,514,516,519],{"class":65,"line":89},[63,505,70],{"class":69},[63,507,74],{"class":73},[63,509,77],{"class":69},[63,511,74],{"class":73},[63,513,83],{"class":82},[63,515,102],{"class":73},[63,517,518],{"class":82}," 3",[63,520,86],{"class":73},[20,522,110],{},[54,524,526],{"className":56,"code":525,"language":58,"meta":59,"style":59},"3.14\n3.142\n",[15,527,528,532],{"__ignoreMap":59},[63,529,530],{"class":65,"line":66},[63,531,125],{"class":82},[63,533,534],{"class":65,"line":89},[63,535,536],{"class":82},"3.142\n",[20,538,539],{},"This is helpful when you want a simpler numeric value for calculations or reporting.",[54,541,543],{"className":56,"code":542,"language":58,"meta":59,"style":59},"price = 19.9876\nrounded_price = round(price, 2)\n\nprint(price)\nprint(rounded_price)\n",[15,544,545,555,575,581,592],{"__ignoreMap":59},[63,546,547,550,552],{"class":65,"line":66},[63,548,549],{"class":382},"price ",[63,551,386],{"class":270},[63,553,554],{"class":82}," 19.9876\n",[63,556,557,560,562,564,566,569,571,573],{"class":65,"line":89},[63,558,559],{"class":382},"rounded_price ",[63,561,386],{"class":270},[63,563,389],{"class":69},[63,565,74],{"class":73},[63,567,568],{"class":181},"price",[63,570,102],{"class":73},[63,572,105],{"class":82},[63,574,397],{"class":73},[63,576,577],{"class":65,"line":254},[63,578,580],{"emptyLinePlaceholder":579},true,"\n",[63,582,584,586,588,590],{"class":65,"line":583},4,[63,585,70],{"class":69},[63,587,74],{"class":73},[63,589,568],{"class":181},[63,591,397],{"class":73},[63,593,595,597,599,602],{"class":65,"line":594},5,[63,596,70],{"class":69},[63,598,74],{"class":73},[63,600,601],{"class":181},"rounded_price",[63,603,397],{"class":73},[20,605,110],{},[54,607,609],{"className":56,"code":608,"language":58,"meta":59,"style":59},"19.9876\n19.99\n",[15,610,611,616],{"__ignoreMap":59},[63,612,613],{"class":65,"line":66},[63,614,615],{"class":82},"19.9876\n",[63,617,618],{"class":65,"line":89},[63,619,620],{"class":82},"19.99\n",[20,622,623,624,627,628,632,633,307],{},"If you need to convert text such as ",[15,625,626],{},"\"3.14159\""," before rounding, see ",[303,629,631],{"href":630},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","how to convert a string to float in Python"," or ",[303,634,13,636,469],{"href":635},"\u002Freference\u002Fpython-float-function-explained\u002F",[15,637,638],{},"float()",[49,640,642,643],{"id":641},"using-negative-ndigits","Using negative ",[15,644,203],{},[20,646,647,648,650],{},"A negative ",[15,649,203],{}," value rounds to tens, hundreds, or larger place values.",[54,652,654],{"className":56,"code":653,"language":58,"meta":59,"style":59},"print(round(1234, -1))\nprint(round(1234, -2))\nprint(round(1234, -3))\n",[15,655,656,677,697],{"__ignoreMap":59},[63,657,658,660,662,664,666,668,670,672,675],{"class":65,"line":66},[63,659,70],{"class":69},[63,661,74],{"class":73},[63,663,77],{"class":69},[63,665,74],{"class":73},[63,667,265],{"class":82},[63,669,102],{"class":73},[63,671,271],{"class":270},[63,673,674],{"class":82},"1",[63,676,86],{"class":73},[63,678,679,681,683,685,687,689,691,693,695],{"class":65,"line":89},[63,680,70],{"class":69},[63,682,74],{"class":73},[63,684,77],{"class":69},[63,686,74],{"class":73},[63,688,265],{"class":82},[63,690,102],{"class":73},[63,692,271],{"class":270},[63,694,274],{"class":82},[63,696,86],{"class":73},[63,698,699,701,703,705,707,709,711,713,716],{"class":65,"line":254},[63,700,70],{"class":69},[63,702,74],{"class":73},[63,704,77],{"class":69},[63,706,74],{"class":73},[63,708,265],{"class":82},[63,710,102],{"class":73},[63,712,271],{"class":270},[63,714,715],{"class":82},"3",[63,717,86],{"class":73},[20,719,110],{},[54,721,723],{"className":56,"code":722,"language":58,"meta":59,"style":59},"1230\n1200\n1000\n",[15,724,725,730,734],{"__ignoreMap":59},[63,726,727],{"class":65,"line":66},[63,728,729],{"class":82},"1230\n",[63,731,732],{"class":65,"line":89},[63,733,298],{"class":82},[63,735,736],{"class":65,"line":254},[63,737,738],{"class":82},"1000\n",[20,740,741],{},"How this works:",[30,743,744,750,756],{},[33,745,746,749],{},[15,747,748],{},"-1"," rounds to the nearest 10",[33,751,752,755],{},[15,753,754],{},"-2"," rounds to the nearest 100",[33,757,758,761],{},[15,759,760],{},"-3"," rounds to the nearest 1000",[20,763,764],{},"This can be useful when you want less precise but easier-to-read numbers.",[49,766,768],{"id":767},"important-behavior-beginners-notice","Important behavior beginners notice",[20,770,771,772,307],{},"Some results may look surprising with numbers ending in ",[15,773,774],{},".5",[20,776,777],{},"For example:",[54,779,781],{"className":56,"code":780,"language":58,"meta":59,"style":59},"print(round(2.5))\nprint(round(3.5))\n",[15,782,783,798],{"__ignoreMap":59},[63,784,785,787,789,791,793,796],{"class":65,"line":66},[63,786,70],{"class":69},[63,788,74],{"class":73},[63,790,77],{"class":69},[63,792,74],{"class":73},[63,794,795],{"class":82},"2.5",[63,797,86],{"class":73},[63,799,800,802,804,806,808,811],{"class":65,"line":89},[63,801,70],{"class":69},[63,803,74],{"class":73},[63,805,77],{"class":69},[63,807,74],{"class":73},[63,809,810],{"class":82},"3.5",[63,812,86],{"class":73},[20,814,110],{},[54,816,818],{"className":56,"code":817,"language":58,"meta":59,"style":59},"2\n4\n",[15,819,820,825],{"__ignoreMap":59},[63,821,822],{"class":65,"line":66},[63,823,824],{"class":82},"2\n",[63,826,827],{"class":65,"line":89},[63,828,364],{"class":82},[20,830,831,832,836,837,839],{},"Python uses ",[833,834,835],"strong",{},"banker's rounding"," in tie cases. That means it rounds to the nearest even number in some ",[15,838,774],{}," situations.",[20,841,842],{},"More examples:",[54,844,846],{"className":56,"code":845,"language":58,"meta":59,"style":59},"print(round(1.5))\nprint(round(2.5))\nprint(round(3.5))\nprint(round(4.5))\n",[15,847,848,863,877,891],{"__ignoreMap":59},[63,849,850,852,854,856,858,861],{"class":65,"line":66},[63,851,70],{"class":69},[63,853,74],{"class":73},[63,855,77],{"class":69},[63,857,74],{"class":73},[63,859,860],{"class":82},"1.5",[63,862,86],{"class":73},[63,864,865,867,869,871,873,875],{"class":65,"line":89},[63,866,70],{"class":69},[63,868,74],{"class":73},[63,870,77],{"class":69},[63,872,74],{"class":73},[63,874,795],{"class":82},[63,876,86],{"class":73},[63,878,879,881,883,885,887,889],{"class":65,"line":254},[63,880,70],{"class":69},[63,882,74],{"class":73},[63,884,77],{"class":69},[63,886,74],{"class":73},[63,888,810],{"class":82},[63,890,86],{"class":73},[63,892,893,895,897,899,901,904],{"class":65,"line":583},[63,894,70],{"class":69},[63,896,74],{"class":73},[63,898,77],{"class":69},[63,900,74],{"class":73},[63,902,903],{"class":82},"4.5",[63,905,86],{"class":73},[20,907,110],{},[54,909,911],{"className":56,"code":910,"language":58,"meta":59,"style":59},"2\n2\n4\n4\n",[15,912,913,917,921,925],{"__ignoreMap":59},[63,914,915],{"class":65,"line":66},[63,916,824],{"class":82},[63,918,919],{"class":65,"line":89},[63,920,824],{"class":82},[63,922,923],{"class":65,"line":254},[63,924,364],{"class":82},[63,926,927],{"class":65,"line":583},[63,928,364],{"class":82},[20,930,931],{},"This is normal Python behavior, not a bug.",[20,933,934],{},"So:",[30,936,937,945],{},[33,938,939,942,943],{},[15,940,941],{},"round(2.5)"," gives ",[15,944,274],{},[33,946,947,942,950],{},[15,948,949],{},"round(3.5)",[15,951,952],{},"4",[20,954,955,956,958],{},"If you expected ",[15,957,774],{}," to always round upward, this behavior can be confusing at first.",[49,960,962],{"id":961},"return-values","Return values",[20,964,965,967],{},[15,966,17],{}," returns the rounded value.",[20,969,970,971,974],{},"It does ",[833,972,973],{},"not"," change the original variable unless you save the result.",[54,976,978],{"className":56,"code":977,"language":58,"meta":59,"style":59},"value = 3.14159\n\nprint(value)\nprint(round(value, 2))\nprint(value)\n",[15,979,980,990,994,1005,1023],{"__ignoreMap":59},[63,981,982,985,987],{"class":65,"line":66},[63,983,984],{"class":382},"value ",[63,986,386],{"class":270},[63,988,989],{"class":82}," 3.14159\n",[63,991,992],{"class":65,"line":89},[63,993,580],{"emptyLinePlaceholder":579},[63,995,996,998,1000,1003],{"class":65,"line":254},[63,997,70],{"class":69},[63,999,74],{"class":73},[63,1001,1002],{"class":181},"value",[63,1004,397],{"class":73},[63,1006,1007,1009,1011,1013,1015,1017,1019,1021],{"class":65,"line":583},[63,1008,70],{"class":69},[63,1010,74],{"class":73},[63,1012,77],{"class":69},[63,1014,74],{"class":73},[63,1016,1002],{"class":181},[63,1018,102],{"class":73},[63,1020,105],{"class":82},[63,1022,86],{"class":73},[63,1024,1025,1027,1029,1031],{"class":65,"line":594},[63,1026,70],{"class":69},[63,1028,74],{"class":73},[63,1030,1002],{"class":181},[63,1032,397],{"class":73},[20,1034,110],{},[54,1036,1038],{"className":56,"code":1037,"language":58,"meta":59,"style":59},"3.14159\n3.14\n3.14159\n",[15,1039,1040,1045,1049],{"__ignoreMap":59},[63,1041,1042],{"class":65,"line":66},[63,1043,1044],{"class":82},"3.14159\n",[63,1046,1047],{"class":65,"line":89},[63,1048,125],{"class":82},[63,1050,1051],{"class":65,"line":254},[63,1052,1044],{"class":82},[20,1054,1055],{},"If you want to keep the rounded value, assign it:",[54,1057,1059],{"className":56,"code":1058,"language":58,"meta":59,"style":59},"value = 3.14159\nvalue = round(value, 2)\n\nprint(value)\n",[15,1060,1061,1069,1087,1091],{"__ignoreMap":59},[63,1062,1063,1065,1067],{"class":65,"line":66},[63,1064,984],{"class":382},[63,1066,386],{"class":270},[63,1068,989],{"class":82},[63,1070,1071,1073,1075,1077,1079,1081,1083,1085],{"class":65,"line":89},[63,1072,984],{"class":382},[63,1074,386],{"class":270},[63,1076,389],{"class":69},[63,1078,74],{"class":73},[63,1080,1002],{"class":181},[63,1082,102],{"class":73},[63,1084,105],{"class":82},[63,1086,397],{"class":73},[63,1088,1089],{"class":65,"line":254},[63,1090,580],{"emptyLinePlaceholder":579},[63,1092,1093,1095,1097,1099],{"class":65,"line":583},[63,1094,70],{"class":69},[63,1096,74],{"class":73},[63,1098,1002],{"class":181},[63,1100,397],{"class":73},[20,1102,110],{},[54,1104,1105],{"className":56,"code":125,"language":58,"meta":59,"style":59},[15,1106,1107],{"__ignoreMap":59},[63,1108,1109],{"class":65,"line":66},[63,1110,125],{"class":82},[49,1112,1114,1115],{"id":1113},"when-to-use-round","When to use ",[15,1116,17],{},[20,1118,128,1119,1121],{},[15,1120,17],{}," when you want to:",[30,1123,1124,1127,1130,1133],{},[33,1125,1126],{},"Display simpler numeric output",[33,1128,1129],{},"Limit decimal places in calculations",[33,1131,1132],{},"Round values before reporting results",[33,1134,1135],{},"Prepare numbers for easier reading",[20,1137,777],{},[54,1139,1141],{"className":56,"code":1140,"language":58,"meta":59,"style":59},"distance = 12.67891\nprint(round(distance, 2))\n",[15,1142,1143,1153],{"__ignoreMap":59},[63,1144,1145,1148,1150],{"class":65,"line":66},[63,1146,1147],{"class":382},"distance ",[63,1149,386],{"class":270},[63,1151,1152],{"class":82}," 12.67891\n",[63,1154,1155,1157,1159,1161,1163,1166,1168,1170],{"class":65,"line":89},[63,1156,70],{"class":69},[63,1158,74],{"class":73},[63,1160,77],{"class":69},[63,1162,74],{"class":73},[63,1164,1165],{"class":181},"distance",[63,1167,102],{"class":73},[63,1169,105],{"class":82},[63,1171,86],{"class":73},[20,1173,110],{},[54,1175,1177],{"className":56,"code":1176,"language":58,"meta":59,"style":59},"12.68\n",[15,1178,1179],{"__ignoreMap":59},[63,1180,1181],{"class":65,"line":66},[63,1182,1176],{"class":82},[49,1184,1186],{"id":1185},"common-mistakes","Common mistakes",[20,1188,1189,1190,307],{},"Here are some common beginner mistakes when using ",[15,1191,17],{},[1193,1194,1196,1197,1199,1200,1202],"h3",{"id":1195},"expecting-round-to-always-round-5-upward","Expecting ",[15,1198,17],{}," to always round ",[15,1201,774],{}," upward",[20,1204,1205],{},"This is one of the most common surprises.",[54,1207,1208],{"className":56,"code":780,"language":58,"meta":59,"style":59},[15,1209,1210,1224],{"__ignoreMap":59},[63,1211,1212,1214,1216,1218,1220,1222],{"class":65,"line":66},[63,1213,70],{"class":69},[63,1215,74],{"class":73},[63,1217,77],{"class":69},[63,1219,74],{"class":73},[63,1221,795],{"class":82},[63,1223,86],{"class":73},[63,1225,1226,1228,1230,1232,1234,1236],{"class":65,"line":89},[63,1227,70],{"class":69},[63,1229,74],{"class":73},[63,1231,77],{"class":69},[63,1233,74],{"class":73},[63,1235,810],{"class":82},[63,1237,86],{"class":73},[20,1239,110],{},[54,1241,1242],{"className":56,"code":817,"language":58,"meta":59,"style":59},[15,1243,1244,1248],{"__ignoreMap":59},[63,1245,1246],{"class":65,"line":66},[63,1247,824],{"class":82},[63,1249,1250],{"class":65,"line":89},[63,1251,364],{"class":82},[20,1253,1254],{},"Python may round tie values to the nearest even number.",[1193,1256,1258],{"id":1257},"forgetting-to-save-the-returned-value","Forgetting to save the returned value",[20,1260,1261,1263],{},[15,1262,17],{}," returns a new value. It does not update the old one by itself.",[54,1265,1267],{"className":56,"code":1266,"language":58,"meta":59,"style":59},"value = 3.14159\nround(value, 2)\n\nprint(value)\n",[15,1268,1269,1277,1291,1295],{"__ignoreMap":59},[63,1270,1271,1273,1275],{"class":65,"line":66},[63,1272,984],{"class":382},[63,1274,386],{"class":270},[63,1276,989],{"class":82},[63,1278,1279,1281,1283,1285,1287,1289],{"class":65,"line":89},[63,1280,77],{"class":69},[63,1282,74],{"class":73},[63,1284,1002],{"class":181},[63,1286,102],{"class":73},[63,1288,105],{"class":82},[63,1290,397],{"class":73},[63,1292,1293],{"class":65,"line":254},[63,1294,580],{"emptyLinePlaceholder":579},[63,1296,1297,1299,1301,1303],{"class":65,"line":583},[63,1298,70],{"class":69},[63,1300,74],{"class":73},[63,1302,1002],{"class":181},[63,1304,397],{"class":73},[20,1306,110],{},[54,1308,1309],{"className":56,"code":1044,"language":58,"meta":59,"style":59},[15,1310,1311],{"__ignoreMap":59},[63,1312,1313],{"class":65,"line":66},[63,1314,1044],{"class":82},[20,1316,1317],{},"To keep the rounded result:",[54,1319,1321],{"className":56,"code":1320,"language":58,"meta":59,"style":59},"value = round(value, 2)\n",[15,1322,1323],{"__ignoreMap":59},[63,1324,1325,1327,1329,1331,1333,1335,1337,1339],{"class":65,"line":66},[63,1326,984],{"class":382},[63,1328,386],{"class":270},[63,1330,389],{"class":69},[63,1332,74],{"class":73},[63,1334,1002],{"class":181},[63,1336,102],{"class":73},[63,1338,105],{"class":82},[63,1340,397],{"class":73},[1193,1342,1344],{"id":1343},"passing-a-string-instead-of-a-number","Passing a string instead of a number",[20,1346,1347],{},"This will cause an error:",[54,1349,1351],{"className":56,"code":1350,"language":58,"meta":59,"style":59},"# print(round(\"3.14\", 1))\n",[15,1352,1353],{"__ignoreMap":59},[63,1354,1355],{"class":65,"line":66},[63,1356,1350],{"class":1357},"sutJx",[20,1359,1360],{},"You need to convert the string first:",[54,1362,1364],{"className":56,"code":1363,"language":58,"meta":59,"style":59},"text = \"3.14\"\nnumber = float(text)\n\nprint(round(number, 1))\n",[15,1365,1366,1382,1399,1403],{"__ignoreMap":59},[63,1367,1368,1371,1373,1376,1379],{"class":65,"line":66},[63,1369,1370],{"class":382},"text ",[63,1372,386],{"class":270},[63,1374,1375],{"class":448}," \"",[63,1377,1378],{"class":452},"3.14",[63,1380,1381],{"class":448},"\"\n",[63,1383,1384,1387,1389,1392,1394,1397],{"class":65,"line":89},[63,1385,1386],{"class":382},"number ",[63,1388,386],{"class":270},[63,1390,1391],{"class":417}," float",[63,1393,74],{"class":73},[63,1395,1396],{"class":181},"text",[63,1398,397],{"class":73},[63,1400,1401],{"class":65,"line":254},[63,1402,580],{"emptyLinePlaceholder":579},[63,1404,1405,1407,1409,1411,1413,1415,1417,1419],{"class":65,"line":583},[63,1406,70],{"class":69},[63,1408,74],{"class":73},[63,1410,77],{"class":69},[63,1412,74],{"class":73},[63,1414,182],{"class":181},[63,1416,102],{"class":73},[63,1418,249],{"class":82},[63,1420,86],{"class":73},[20,1422,110],{},[54,1424,1426],{"className":56,"code":1425,"language":58,"meta":59,"style":59},"3.1\n",[15,1427,1428],{"__ignoreMap":59},[63,1429,1430],{"class":65,"line":66},[63,1431,1425],{"class":82},[1193,1433,1435],{"id":1434},"confusing-rounding-a-value-with-formatting-text-output","Confusing rounding a value with formatting text output",[20,1437,1438,1440],{},[15,1439,17],{}," changes the numeric value it returns. It is not the same as formatting text for display.",[20,1442,1443,1444,632,1448,307],{},"If your data starts as text, convert it first with ",[303,1445,1446],{"href":635},[15,1447,638],{},[303,1449,1450],{"href":465},[15,1451,468],{},[49,1453,1455],{"id":1454},"faq","FAQ",[1193,1457,1459,1460,1462,1463,1466],{"id":1458},"does-round-always-round-05-up","Does ",[15,1461,17],{}," always round ",[15,1464,1465],{},"0.5"," up?",[20,1468,1469],{},"No. In tie cases, Python may round to the nearest even number.",[1193,1471,1459,1473,1475],{"id":1472},"does-round-change-the-original-number",[15,1474,17],{}," change the original number?",[20,1477,1478],{},"No. It returns a new value. You must assign it if you want to keep it.",[1193,1480,1482,1483,1485],{"id":1481},"can-round-round-to-tens-or-hundreds","Can ",[15,1484,17],{}," round to tens or hundreds?",[20,1487,1488,1489,1491,1492,632,1494,307],{},"Yes. Use a negative ",[15,1490,203],{}," value like ",[15,1493,748],{},[15,1495,754],{},[1193,1497,1499,1500,1502,1503,1506],{"id":1498},"can-i-use-round-on-a-string-like-314","Can I use ",[15,1501,17],{}," on a string like ",[15,1504,1505],{},"\"3.14\"","?",[20,1508,1509,1510,632,1512,307],{},"Not directly. Convert the string first with ",[15,1511,638],{},[15,1513,468],{},[49,1515,1517],{"id":1516},"see-also","See also",[30,1519,1520,1526,1532,1536],{},[33,1521,1522],{},[303,1523,13,1524,469],{"href":635},[15,1525,638],{},[33,1527,1528],{},[303,1529,13,1530,469],{"href":465},[15,1531,468],{},[33,1533,1534],{},[303,1535,306],{"href":305},[33,1537,1538],{},[303,1539,1540],{"href":630},"How to convert a string to float in Python",[1542,1543,1544],"style",{},"html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}",{"title":59,"searchDepth":89,"depth":89,"links":1546},[1547,1548,1550,1551,1553,1555,1557,1558,1559,1561,1568,1578],{"id":51,"depth":89,"text":52},{"id":137,"depth":89,"text":1549},"What round() does",{"id":163,"depth":89,"text":164},{"id":310,"depth":89,"text":1552},"Using round() with one argument",{"id":472,"depth":89,"text":1554},"Using round() with decimal places",{"id":641,"depth":89,"text":1556},"Using negative ndigits",{"id":767,"depth":89,"text":768},{"id":961,"depth":89,"text":962},{"id":1113,"depth":89,"text":1560},"When to use round()",{"id":1185,"depth":89,"text":1186,"children":1562},[1563,1565,1566,1567],{"id":1195,"depth":254,"text":1564},"Expecting round() to always round .5 upward",{"id":1257,"depth":254,"text":1258},{"id":1343,"depth":254,"text":1344},{"id":1434,"depth":254,"text":1435},{"id":1454,"depth":89,"text":1455,"children":1569},[1570,1572,1574,1576],{"id":1458,"depth":254,"text":1571},"Does round() always round 0.5 up?",{"id":1472,"depth":254,"text":1573},"Does round() change the original number?",{"id":1481,"depth":254,"text":1575},"Can round() round to tens or hundreds?",{"id":1498,"depth":254,"text":1577},"Can I use round() on a string like \"3.14\"?",{"id":1516,"depth":89,"text":1517},"Master python round function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-round-function-explained",{"title":5,"description":1579},"reference\u002Fpython-round-function-explained","e0t8hwZaUiwtmjnn3D5wfXRBmBmXDud37ro5ZcnR9iM",1777585497995]