{"id":30862,"date":"2025-04-30T09:39:56","date_gmt":"2025-04-30T09:39:56","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/sagex3\/?p=30862"},"modified":"2025-04-30T10:06:13","modified_gmt":"2025-04-30T10:06:13","slug":"how-to-convert-a-string-char-to-integer-or-decimal-in-sage-x3","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/04\/30\/how-to-convert-a-string-char-to-integer-or-decimal-in-sage-x3\/","title":{"rendered":"How to Convert a String (Char) to Integer or Decimal in Sage X3"},"content":{"rendered":"\n<p>In <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" rel=\"noreferrer noopener\">Sage X3<\/a>, converting string data into numeric values is a common requirement\u2014especially when working with imported data, user inputs, or web services. The val() function makes this conversion simple and efficient. In this blog post, we&#8217;ll walk through how to use the val() function, handle invalid inputs, and perform basic arithmetic operations with it.<\/p>\n\n\n\n<p><strong>What is the val() Function?<\/strong><br>The val() function is a built-in function in Sage X3 that allows you to convert a string (Char) into a numeric value. You can then use that value for calculations or comparisons.<\/p>\n\n\n\n<p><strong>Example 1<\/strong>: Convert a Valid String to Integer and Perform Calculations<br>In this example, we\u2019ll convert a valid numeric string to an integer and multiply it by 5.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Variables<\/h2>\n\n\n\n<p>Local Char NumStr : NumStr = &#8220;2&#8221;<br>Local Integer NumInt : NumInt = 0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Convert string to integer<\/h2>\n\n\n\n<p>NumInt = val(NumStr) * 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Print the result<\/h2>\n\n\n\n<p>Infbox &#8220;The result is: &#8221; + NumStr + &#8221; * 5 = &#8221; + num$(NumInt)<\/p>\n\n\n\n<p><strong>Output:<\/strong><br>The result is: 2 * 5 = 10<\/p>\n\n\n\n<p><strong>In this example:<\/strong><br>1.We defined a string variable NumStr with the value &#8220;2&#8221;<br>2.We converted it to an integer using val(NumStr)<br>3.We multiplied the result by 5<br>4.Finally, we displayed the result using Infbox<\/p>\n\n\n\n<p><strong>New Stuff:-<\/strong> <a href=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/04\/29\/custom-field-entry-without-sales-invoice-access-in-sage-x3\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/04\/29\/custom-field-entry-without-sales-invoice-access-in-sage-x3\/\" rel=\"noreferrer noopener\">Custom Field Entry Without Sales Invoice Access in Sage X3<\/a><\/p>\n\n\n\n<p><strong>What Happens with Invalid Strings?<\/strong><br>If the string contains non-numeric characters, the val() function will return 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example with Invalid Input:<\/strong><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">## Variables<\/h2>\n\n\n\n<p>Local Char NumStr : NumStr = &#8220;str2&#8221;<br>Local Integer NumInt : NumInt = 0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Convert string to integer<\/h2>\n\n\n\n<p>NumInt = val(NumStr) * 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Print the result<\/h2>\n\n\n\n<p>Infbox &#8220;The result is: &#8221; + NumStr + &#8221; * 5 = &#8221; + num$(NumInt)<\/p>\n\n\n\n<p><strong>Output:<\/strong><br>The result is: str2 * 5 = 0<\/p>\n\n\n\n<p>Here, since the string starts with alphabetic characters, the conversion fails and returns 0.<\/p>\n\n\n\n<p>What If the String Starts with a Number?<br>If the string starts with a numeric character followed by text, val() will extract and convert only the leading numeric part.<\/p>\n\n\n\n<p><strong>Example with Mixed Input:<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Variables<\/h2>\n\n\n\n<p>Local Char NumStr : NumStr = &#8220;2str2&#8221;<br>Local Integer NumInt : NumInt = 0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Convert string to integer<\/h2>\n\n\n\n<p>NumInt = val(NumStr) * 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">## Print the result<\/h2>\n\n\n\n<p>Infbox &#8220;The result is: &#8221; + NumStr + &#8221; * 5 = &#8221; + num$(NumInt)<\/p>\n\n\n\n<p><strong>Output:<\/strong><br>The result is: 2str2 * 5 = 10<\/p>\n\n\n\n<p><strong>Here, only the first numeric part (2) is considered, and the rest is ignored.<\/strong><\/p>\n\n\n\n<p>If you&#8217;re working with decimal numbers (e.g., &#8220;3.14&#8221;), make sure to use a Decimal or Real variable to store the result. If you use an Integer, the decimal portion will be truncated.<\/p>\n\n\n\n<p><strong>Code:<\/strong><br>Local Char NumStr : NumStr = &#8220;3.14&#8221;<br>Local Decimal NumDecimal : NumDecimal = 0.0<\/p>\n\n\n\n<p>NumDecimal = val(NumStr)<\/p>\n\n\n\n<p>In Sage X3, this is how we convert a string (char) to an integer or decimal value.<\/p>\n\n\n[about_us_blog_common]\n","protected":false},"excerpt":{"rendered":"<p>In Sage X3, converting string data into numeric values is a common requirement\u2014especially when working with imported data, user inputs, or web services. The val() function makes this conversion simple and efficient. In this blog post, we&#8217;ll walk through how to use the val() function, handle invalid inputs, and perform basic arithmetic operations with it.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/04\/30\/how-to-convert-a-string-char-to-integer-or-decimal-in-sage-x3\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8],"tags":[4370,4364,4368,4369,4367,4363,4366,4365],"class_list":["post-30862","post","type-post","status-publish","format-standard","hentry","category-integration","category-sage-erp-x3","tag-arithmetic-operations-in-sage-x3","tag-char-to-integer-in-sage-x3","tag-data-type-conversion-in-sage-x3","tag-sage-x3-scripting-tips","tag-string-to-decimal-in-sage-x3","tag-string-to-integer-in-sage-x3","tag-using-val-in-sage-x3","tag-val-function-in-sage-x3"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/comments?post=30862"}],"version-history":[{"count":6,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30862\/revisions"}],"predecessor-version":[{"id":30872,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30862\/revisions\/30872"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/media?parent=30862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/categories?post=30862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/tags?post=30862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}