{"id":12172,"date":"2020-12-03T10:09:49","date_gmt":"2020-12-03T18:09:49","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=12172"},"modified":"2023-12-01T04:05:35","modified_gmt":"2023-12-01T12:05:35","slug":"python-round","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-round\/","title":{"rendered":"Python Round: A Step-By-Step Guide"},"content":{"rendered":"\n<p><em>The Python round() method rounds a number to a specified number of decimal places. By default, round() rounds a number to zero decimal places. <\/em><em>round<\/em><em>() is useful if you want to work with numbers with a certain precision.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>You may encounter a scenario where you want to round a number to a certain decimal place.<\/p>\n\n\n\n<p>You can use the <em>round()<\/em> function in Python to do this. For example, you may be running a butcher shop and want to round the prices of your products to the nearest dollar.&nbsp;<\/p>\n\n\n\n<p>In this tutorial, we will explore how to round numbers using the <em>round()<\/em> function in Python. We\u2019ll also go through a few examples to illustrate how this function would work in an actual program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python round() Method<\/h2>\n\n\n\n<p>The Python round() method rounds a number to a specific decimal place. This method returns a floating-point number rounded to your specifications. By default, the round() method rounds a number to zero decimal places.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the syntax for the round() built-in function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>round(number_to_round, decimal_places)<\/pre><\/div>\n\n\n\n<p>round() accepts two arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The value you want to round.<\/li><li>The number of decimal points to which the value should be rounded (optional, default zero decimal places).<\/li><\/ul>\n\n\n\n<p>round() returns a floating-point because the method can return decimal values. Thus, you should plan to accept a float instead of an integer number. This is important to consider if you decide to perform math on the result of the round() method.<\/p>\n\n\n\n<p>Python does have two methods that let you round up and down. The floor() method rounds down to the nearest integer. ceil() rounds up to the nearest integer. We&#8217;ll focus on the round() method in this guide.<\/p>\n\n\n\n<p>You can read more about ceil() and floor() in our <a href=\"https:\/\/careerkarma.com\/blog\/python-floor-ceil\/\">Python guide to ceil() and floor()<\/a>.<\/p>\n\n\n\n<p>One of the most common uses of rounding numbers is when you\u2019re working with money. This is because many people prefer not to work with fractions of pennies and decimal fractions when working with money.<\/p>\n\n\n\n<p>Rounding numbers is also common when you\u2019re working with mathematical calculations that return unrounded values. You&#8217;ll find rounding values comes up a lot in data science and machine learning for this reason.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">round() Python: Example<\/h2>\n\n\n\n<p>Let\u2019s use an example to illustrate how this function works. Say that we manage a produce stand and are looking to calculate how we should price individual oranges.<\/p>\n\n\n\n<p>We buy oranges in bundles of fifty but sell single oranges to customers. So, we want to divide the price of fifty oranges by fifty to get the wholesale price of an individual orange.<\/p>\n\n\n\n<p>Here\u2019s an example Python script we could use to accomplish this task:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>fifty_oranges = 15.52\n\nindividual_orange = fifty_oranges \/ 50\nprint(individual_orange)\n\nrounded_value = round(individual_orange, 2)\nprint(rounded_value)<\/pre><\/div>\n\n\n\n<p>Our code returns a <a href=\"https:\/\/careerkarma.com\/blog\/python-float\/\">Python floating-point number<\/a> followed by a rounded number:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>0.3104\n0.31<\/pre><\/div>\n\n\n\n<p>First, we declared the price of fifty oranges. We set this value as a <a href=\"https:\/\/careerkarma.com\/blog\/python-variables\/\">Python variable<\/a>, whose value is $15.52 in this case. Then, we calculated the price of an individual orange by dividing the price of fifty oranges by fifty.&nbsp;<\/p>\n\n\n\n<p>However, the price of an individual orange turned out to be $0.3104, which is not a price that we can charge consumers. So, we used the <em>round()<\/em> function to return the price of an individual orange rounded to the nearest penny.<\/p>\n\n\n\n<p>Our program returned the rounded number 0.31, which tells us that we want to charge more than $0.31 per orange to make a profit.<\/p>\n\n\n\n<p>In our example, we specified <em>2<\/em> in our <em>round()<\/em> method, which tells our program to round our number to two decimal places. However, if we wanted the <em>round()<\/em> function to return the nearest integer, we would not need to specify this parameter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Round to The Nearest Whole Number<\/h2>\n\n\n\n<p>To round to the nearest whole number in Python, you can use the round() method. You should not specify a number of decimal places to which your number should be rounded.<\/p>\n\n\n\n<p>Here\u2019s an example of using <em>round()<\/em> without the second parameter to round a number to its nearest whole value:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>number = 24.28959\nprint(round(number))<\/pre><\/div>\n\n\n\n<p>Our Python code returns: <em>24<\/em>.<\/p>\n\n\n\n<p>The <em>round()<\/em> function is usually used with floating-point numbers, which include decimal places. You can also pass integers into <em>round()<\/em> without returning an error. With that said, our program will return the same values because integers are already round numbers.<\/p>\n\n\n\n<p>Optionally, you can specify 0 as a second argument instead of omitting it entirely. This will also round a value to the nearest whole number. But since the 0 is the default value, we did not specify it in this example.<\/p>\n\n\n\n<p>The <em>round()<\/em> function can also be used to round a negative number. Here\u2019s an example of this in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print(round(-102.22))<\/pre><\/div>\n\n\n\n<p>Our code returns: <em>-102<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You can use the Python <em>round()<\/em> function to round a number to a certain decimal place. This can be helpful if you\u2019re working with mathematical calculations that return numbers with unnecessary decimal places. Or, round() can be helpful if you are working with values like money which typically do not show more than two decimal places.<\/p>\n\n\n\n<p>In this tutorial, we discussed how to use the <em>round()<\/em> function to round numbers in Python. We then explored a few examples using the <em>round()<\/em> function to show how it works. Now you\u2019re ready to start rounding numbers in Python like an expert!<\/p>\n\n\n\n<p>To learn more about Python programming, read our complete <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"The Python round() method rounds a number to a specified number of decimal places. By default, round() rounds a number to zero decimal places. round() is useful if you want to work with numbers with a certain precision. You may encounter a scenario where you want to round a number to a certain decimal place.&hellip;","protected":false},"author":240,"featured_media":11907,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-12172","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Round: A Step-By-Step Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-round\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Round: A Step-By-Step Guide\" \/>\n<meta property=\"og:description\" content=\"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-round\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-03T18:09:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:05:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Round: A Step-By-Step Guide\",\"datePublished\":\"2020-12-03T18:09:49+00:00\",\"dateModified\":\"2023-12-01T12:05:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/\"},\"wordCount\":888,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-round\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-round\/\",\"name\":\"Python Round: A Step-By-Step Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg\",\"datePublished\":\"2020-12-03T18:09:49+00:00\",\"dateModified\":\"2023-12-01T12:05:35+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-round\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg\",\"width\":1000,\"height\":667},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-round\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Round: A Step-By-Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Round: A Step-By-Step Guide | Career Karma","description":"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-round\/","og_locale":"en_US","og_type":"article","og_title":"Python Round: A Step-By-Step Guide","og_description":"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.","og_url":"https:\/\/careerkarma.com\/blog\/python-round\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-03T18:09:49+00:00","article_modified_time":"2023-12-01T12:05:35+00:00","og_image":[{"width":1000,"height":667,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-round\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Round: A Step-By-Step Guide","datePublished":"2020-12-03T18:09:49+00:00","dateModified":"2023-12-01T12:05:35+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/"},"wordCount":888,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-round\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-round\/","url":"https:\/\/careerkarma.com\/blog\/python-round\/","name":"Python Round: A Step-By-Step Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg","datePublished":"2020-12-03T18:09:49+00:00","dateModified":"2023-12-01T12:05:35+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Python round method can be used to round a number to a decimal place. Learn how the round method works on Career Karma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-round\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-round\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-round\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/JavaScript.jpg","width":1000,"height":667},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-round\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Round: A Step-By-Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=12172"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/11907"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=12172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=12172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=12172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}