{"id":19780,"date":"2020-07-19T04:48:17","date_gmt":"2020-07-19T11:48:17","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19780"},"modified":"2020-12-29T13:40:58","modified_gmt":"2020-12-29T21:40:58","slug":"javascript-round","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/javascript-round\/","title":{"rendered":"JavaScript Math Object: Round"},"content":{"rendered":"\n<p><em>This article will show you how to use the Math Object to figure out how a floating point number rounds to the nearest integer.<\/em><br><\/p>\n\n\n\n<p>As a reminder, when we round a number, we look to the right of the decimal point to figure out if we round up or round down. If it\u2019s .5 or more and positive, we round up; if it\u2019s less than .5 when positive, we round down. In the case of negative numbers, the threshold is adjusted slightly:&nbsp; if .5 or less, we round down. Here\u2019s the syntax:<br><\/p>\n\n\n\n<p><code>Math.round(inputVal);<br><\/code><\/p>\n\n\n\n<p>Since we are using the Math Object, we start with Math. The round method comes next. The set of parentheses invokes the method on the input value inside the parentheses and returns the rounded value.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n &lt;head&gt;\n   &lt;meta charset=&quot;utf-8&quot;&gt;\n   &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot;&gt;\n   &lt;title&gt;repl.it&lt;\/title&gt;\n   &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; \/&gt;\n &lt;\/head&gt;\n &lt;body&gt;\n   &lt;form onsubmit=handleSubmit(event)&gt;\n     &lt;label for=&quot;round&quot;&gt;Enter a number:&lt;\/label&gt;\n     &lt;input id=&quot;round&quot; onchange=handleChange(event) type=&quot;text&quot; name=&quot;round&quot; value=&quot;&quot;\/&gt;\n     &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; \/&gt;\n   &lt;\/form&gt;\n \n   &lt;h3 id=&quot;root&quot;&gt;&lt;\/h3&gt;\n \n   &lt;script&gt;\n     let inputVal = &quot;&quot;\n \n     const handleChange = e =&gt; {\n \n       inputVal = e.target.value;\n       console.log(inputVal)\n     }\n     const handleSubmit = e =&gt; {\n       e.preventDefault();\n       const root = document.querySelector(&quot;#root&quot;);\n       root.innerHTML = Math.round(inputVal);\n \n     }\n     const inputValue = document.getElementById(&quot;round&quot;).value\n   &lt;\/script&gt;\n &lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>If you were to enter a string or null into the input, it returns NaN.Try the code editor above and input some floating point numbers to get an idea of how the Math.round() method works.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s it! You can now use the Math object to figure out how to round a number. Here\u2019s some articles that can help you figure out what to learn next:&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-random-number\/\">JavaScript Random Number: A Complete Guide<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-parseint\/\">JavaScript ParseInt: A Step-By-Step Guide<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-countdown-timer\/\">JavaScript Countdown Timer: A Tutorial<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-tostring\/\">JavaScript toString<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-touppercase-tolowercase\/\">JavaScript toUpperCase and toLowerCase<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"This article will show you how to use the Math Object to figure out how a floating point number rounds to the nearest integer. As a reminder, when we round a number, we look to the right of the decimal point to figure out if we round up or round down. If it\u2019s .5 or&hellip;","protected":false},"author":77,"featured_media":19781,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-19780","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","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>JavaScript Math Object: Round | Career Karma<\/title>\n<meta name=\"description\" content=\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.\" \/>\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\/javascript-round\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Math Object: Round\" \/>\n<meta property=\"og:description\" content=\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/javascript-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-07-19T11:48:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T21:40:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\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=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"JavaScript Math Object: Round\",\"datePublished\":\"2020-07-19T11:48:17+00:00\",\"dateModified\":\"2020-12-29T21:40:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/\"},\"wordCount\":226,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/javascript-round\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/\",\"name\":\"JavaScript Math Object: Round | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg\",\"datePublished\":\"2020-07-19T11:48:17+00:00\",\"dateModified\":\"2020-12-29T21:40:58+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/javascript-round\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg\",\"width\":1000,\"height\":750},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/javascript-round\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/careerkarma.com\/blog\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Math Object: Round\"}]},{\"@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\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\/\/www.linkedin.com\/in\/cmvnk\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JavaScript Math Object: Round | Career Karma","description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.","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\/javascript-round\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Math Object: Round","og_description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.","og_url":"https:\/\/careerkarma.com\/blog\/javascript-round\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-19T11:48:17+00:00","article_modified_time":"2020-12-29T21:40:58+00:00","og_image":[{"width":1000,"height":750,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"JavaScript Math Object: Round","datePublished":"2020-07-19T11:48:17+00:00","dateModified":"2020-12-29T21:40:58+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/"},"wordCount":226,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/javascript-round\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/","url":"https:\/\/careerkarma.com\/blog\/javascript-round\/","name":"JavaScript Math Object: Round | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg","datePublished":"2020-07-19T11:48:17+00:00","dateModified":"2020-12-29T21:40:58+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the round method. In this article, we\u2019ll learn how to use the Math.round()method with examples.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/javascript-round\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/photo-1509592931695-18ef24228b9d.jpg","width":1000,"height":750},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/javascript-round\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Math Object: Round"}]},{"@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\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19780","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\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19780"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19780\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19781"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}