{"id":43621,"date":"2022-06-17T10:30:33","date_gmt":"2022-06-17T10:30:33","guid":{"rendered":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/?p=43621"},"modified":"2024-06-27T13:35:18","modified_gmt":"2024-06-27T13:35:18","slug":"js-tips-how-to-round-a-number","status":"publish","type":"post","link":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/","title":{"rendered":"JS tips &#8211; How to round a number"},"content":{"rendered":"\n<p>While our powerful form builder offers the ability to make calculations with the <a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/form-calculations\/\">Formula field<\/a>, set a <a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/can-i-set-a-minimum-or-a-maximum-value-for-numeric-fields\/\">minimum\/maximum value for a number<\/a> or <a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/simple-field-validation-for-online-forms\/\">change the accepted data format<\/a>, to round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity aligncenter\" \/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-rounding-a-non-integer-number\">Rounding a non-integer number <\/h2>\n\n\n\n<p>Let&#8217;s take the following script as an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function roundResult() {\nvar result1 = loader.engine.document.getElementById(<strong>111111111<\/strong>).getProperty('value.value'); \nvar roundedValue1 = Math.round(result1 * 1) \/ 1;\nloader.engine.document.getElementById(<strong>111111111<\/strong>).setValue(({\"value\": roundedValue1})); \n};\n\nwindow.onclick = roundResult;<\/code><\/pre>\n\n\n\n<p>This script is used to change a fractional number (a number with decimals) into an integer (a whole number). For example, 3.123 becomes 3, while 3.789 becomes 4. <\/p>\n\n\n\n<p>Change the numbers &#8220;<strong>111111111<\/strong>&#8221; with your own field&#8217;s ID (<a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/how-to-pass-user-input-from-one-form-to-another\/\">see how to find the field ID at pt. 2 here<\/a>). Replace with the same field ID in both places, if you want the rounding to be in the same field. If you want to display the fractional number in a field and in another field display its rounded counterpart, then simply add the first field&#8217;s ID in place of the first <strong>111111111<\/strong> and the second field&#8217;s ID in the place of the second <strong>111111111<\/strong>.<\/p>\n\n\n\n<p>See how it works:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video autoplay controls loop src=\"https:\/\/staticresources123.s3.us-west-2.amazonaws.com\/docs\/Videos\/roundingNumberDemo.mp4\"><\/video><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Rounding up and displaying only the first two decimals<\/h2>\n\n\n\n<p>Let&#8217;s say you need to display the prices on your form in a more currency friendly format. You want to avoid numbers such as 25.45678 and display this instead $25.46.<\/p>\n\n\n\n<p>First, make sure you add the currency symbol of your choice with our <b>Prefix <\/b>option (<a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/how-to-add-custom-prefix-in-a-field\/\">learn how here<\/a>).<\/p>\n\n\n\n<p>Second, you will need to use this script on your <b>Formula<\/b> or <b>Number<\/b> field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function roundResult() {\nvar result1 = loader.engine.document.getElementById(<strong>111111111<\/strong>).getProperty('value.value'); \nvar roundedValue1 = +((Math.round(result1 * 100)\/100).toFixed(2));\nloader.engine.document.getElementById(<strong>111111111<\/strong>).setValue(({\"value\": roundedValue1})); \n};\n\nwindow.onclick = roundResult;<\/code><\/pre>\n\n\n\n<p>The same instructions apply as for the previous script.<\/p>\n\n\n\n<p>See how it works:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/staticresources123.s3.us-west-2.amazonaws.com\/docs\/Videos\/roundingTwoDecimals.mp4\"><\/video><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to use these scripts on your own form, after you&#8217;ve added your own field IDs, paste the URL containing the script file into the <b>Advanced<\/b> \u2192 <b>Form<\/b> \u2192 &nbsp;<b>Add a JS script to your form<\/b>. See our documentation on how to add JS to your form <a href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/can-i-add-a-custom-js-script-to-my-form\/\">here.<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"warningBox\"><span style=\"font-size: 16px\">Note: The script examples provided above trigger the action to round the number when a click is made on the form page. You can however change that. E.g: instead of &#8220;window.onclick&#8221; you can use &#8220;window.onchange&#8221; so that the script is triggered when an input is changed in your form.<\/span><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While our powerful form builder offers the ability to make calculations with the Formula field, set a minimum\/maximum value for a number or change the accepted data format, to round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do [&hellip;]<\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[93,3],"tags":[973,974,972,975],"class_list":["post-43621","post","type-post","status-publish","format-standard","hentry","category-advanced","category-documentation","tag-js-tips","tag-round-a-non-integer-number","tag-round-a-number","tag-round-and-show-first-two-decimals"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v24.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>JS tips - How to round a number on your form | 123FormBuilder<\/title>\n<meta name=\"description\" content=\"To round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JS tips - How to round a number\" \/>\n<meta property=\"og:description\" content=\"To round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/\" \/>\n<meta property=\"og:site_name\" content=\"123FormBuilder Knowledge Base\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-17T10:30:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-27T13:35:18+00:00\" \/>\n<meta name=\"author\" content=\"Alexandra Cufteac\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alexandra Cufteac\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JS tips - How to round a number on your form | 123FormBuilder","description":"To round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!","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:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/","og_locale":"en_US","og_type":"article","og_title":"JS tips - How to round a number","og_description":"To round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!","og_url":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/","og_site_name":"123FormBuilder Knowledge Base","article_published_time":"2022-06-17T10:30:33+00:00","article_modified_time":"2024-06-27T13:35:18+00:00","author":"Alexandra Cufteac","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Alexandra Cufteac","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/#article","isPartOf":{"@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/"},"author":{"name":"Alexandra Cufteac","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/#\/schema\/person\/a06666451f3a2183ee005d82f917d6aa"},"headline":"JS tips &#8211; How to round a number","datePublished":"2022-06-17T10:30:33+00:00","dateModified":"2024-06-27T13:35:18+00:00","mainEntityOfPage":{"@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/"},"wordCount":373,"commentCount":0,"keywords":["js tips","round a non-integer number","round a number","round and show first two decimals"],"articleSection":["Advanced","Documentation"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/","url":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/","name":"JS tips - How to round a number on your form | 123FormBuilder","isPartOf":{"@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/#website"},"datePublished":"2022-06-17T10:30:33+00:00","dateModified":"2024-06-27T13:35:18+00:00","author":{"@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/#\/schema\/person\/a06666451f3a2183ee005d82f917d6aa"},"description":"To round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!","breadcrumb":{"@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/js-tips-how-to-round-a-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/"},{"@type":"ListItem","position":2,"name":"JS tips &#8211; How to round a number"}]},{"@type":"WebSite","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/#website","url":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/","name":"123FormBuilder Knowledge Base","description":"Search our documentation platform for answers, service specifications and 3rd-party integrations.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/#\/schema\/person\/a06666451f3a2183ee005d82f917d6aa","name":"Alexandra Cufteac","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/aff6b9df250996a53212a23829abfa7e770f129d62867ed3bd56aee50333469b?s=96&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FAlexandra%2BCufteac%2F96%2Ff3f3f3%2Faaa%2F1%2F0.5%2Ffalse%2Ftrue&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/aff6b9df250996a53212a23829abfa7e770f129d62867ed3bd56aee50333469b?s=96&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FAlexandra%2BCufteac%2F96%2Ff3f3f3%2Faaa%2F1%2F0.5%2Ffalse%2Ftrue&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aff6b9df250996a53212a23829abfa7e770f129d62867ed3bd56aee50333469b?s=96&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FAlexandra%2BCufteac%2F96%2Ff3f3f3%2Faaa%2F1%2F0.5%2Ffalse%2Ftrue&r=g","caption":"Alexandra Cufteac"},"url":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/author\/alexandra-cufteac123formbuilder-com\/"}]}},"_links":{"self":[{"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/posts\/43621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/comments?post=43621"}],"version-history":[{"count":15,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/posts\/43621\/revisions"}],"predecessor-version":[{"id":49039,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/posts\/43621\/revisions\/49039"}],"wp:attachment":[{"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/media?parent=43621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/categories?post=43621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/123formbuilder.polarbuildingcleaning.net\/docs\/wp-json\/wp\/v2\/tags?post=43621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}