{"id":1366,"date":"2022-03-03T07:36:01","date_gmt":"2022-03-03T07:36:01","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1366"},"modified":"2022-03-03T07:36:01","modified_gmt":"2022-03-03T07:36:01","slug":"check-if-a-string-is-numeric","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/check-if-a-string-is-numeric\/","title":{"rendered":"Check if a String is Numeric"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Check if a string is numeric<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Numeric {\n\n    public static void main(String&#91;] args) {\n\n        String string = \"12345.15\";\n        boolean numeric = true;\n\n        try {\n            Double num = Double.parseDouble(string);\n        } catch (NumberFormatException e) {\n            numeric = false;\n        }\n\n        if(numeric)\n            System.out.println(string + \" is a number\");\n        else\n            System.out.println(string + \" is not a number\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>12345.15 is a number<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have a&nbsp;<code>String<\/code>&nbsp;named&nbsp;<var>string<\/var>&nbsp;that contains the string to be checked. We also have a boolean value&nbsp;<var>numeric<\/var>&nbsp;which stores if the final result is numeric or not.<\/p>\n\n\n\n<p>To check if the&nbsp;<var>string<\/var>&nbsp;contains numbers only, in the try block, we use&nbsp;<code>Double<\/code>&#8216;s&nbsp;<code>parseDouble()<\/code>&nbsp;method to convert the string to a&nbsp;<code>Double<\/code>.<\/p>\n\n\n\n<p>If it throws an error (i.e.&nbsp;<code>NumberFormatException<\/code>&nbsp;error), it means the&nbsp;<var>string<\/var>&nbsp;isn&#8217;t a number and&nbsp;<var>numeric<\/var>&nbsp;is set to&nbsp;<code>false<\/code>. Else, it&#8217;s a number.<\/p>\n\n\n\n<p>However, if you want to check if for a number of strings, you would need to change it to a function. And, the logic is based on throwing exceptions, this can be pretty expensive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check if a string is numeric Output In the above program, we have a&nbsp;String&nbsp;named&nbsp;string&nbsp;that contains the string to be checked. We also have a boolean value&nbsp;numeric&nbsp;which stores if the final result is numeric or not. To check if the&nbsp;string&nbsp;contains numbers only, in the try block, we use&nbsp;Double&#8216;s&nbsp;parseDouble()&nbsp;method to convert the string to a&nbsp;Double. If it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[539],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1366"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1366"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1366\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}