

{"id":112695,"date":"2023-05-06T03:48:27","date_gmt":"2023-05-05T22:18:27","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=112695"},"modified":"2023-05-06T15:48:04","modified_gmt":"2023-05-06T10:18:04","slug":"numbers-in-typescript","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/","title":{"rendered":"Numbers in TypeScript"},"content":{"rendered":"<p>In TypeScript, numbers are a primitive data type used to represent numerical values. The number type can hold both integers and floating-point numbers.<\/p>\n<p>TypeScript supports basic arithmetic operations, such as addition, subtraction, multiplication, and division. It also supports more advanced mathematical operations, such as trigonometric functions, logarithms, and rounding.<\/p>\n<p>In addition to the number type, TypeScript supports other numerical types, such as BigInt, which can hold arbitrary length integers, and Math.Random, which provides a random number generator.<\/p>\n<p>When working with numbers in TypeScript, knowing the potential issues related to precision and rounding errors is important. Using appropriate data types and functions is also important to ensure your code is correct and efficient.<\/p>\n<p>Overall, TypeScript provides robust support for working with numerical data, including basic arithmetic operations, advanced mathematical functions, and support for various numerical types.<\/p>\n<h3>Basic Number Types in TypeScript<\/h3>\n<p>The four fundamental types of numbers in TypeScript are number, bigint, float, and double. The most used number type, represents a 64-bit floating-point value. This indicates that a wide range of values can be stored, including both extremely small and very large numbers. Here&#8217;s an illustration of how to use the number type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_x: number = 42;\r\nlet DataFlair_y: number = 3.14;\r\n<\/pre>\n<p>The bigint type is a relatively new addition to TypeScript, introduced in version 3.2. It represents an integer value of arbitrary size. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_big: bigint = 9007199254740991;\r\n<\/pre>\n<p>The float and double types represent floating-point numbers of 32 bits and 64 bits, respectively. Here&#8217;s an example of using the float type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_a: float = 3.14159265359;\r\n<\/pre>\n<h3>Number Literals in TypeScript<\/h3>\n<p>TypeScript also supports number literals, which allow you to specify a specific value for a number variable. Number literals can be written in decimal, binary, octal, and hexadecimal notation. Here&#8217;s an example of using a number literal in TypeScript:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_binary: number = 0b1010; \/\/ binary notation\r\nlet DataFlair_octal: number = 0o744; \/\/ octal notation\r\nlet DataFlair_decimal: number = 6; \/\/ decimal notation\r\nlet DataFlair_hexadecimal: number = 0x7b; \/\/ hexadecimal notation\r\n<\/pre>\n<h3>NaN and Infinity in TypeScript<\/h3>\n<p>TypeScript also supports special numeric values, such as NaN and Infinity. NaN represents a value that is not a number, while Infinity represents a value that is infinitely large. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_notANumber: number = NaN;\r\nlet DataFlair_infinity: number = Infinity;\r\n<\/pre>\n<h3>MIN_VAL and MAX_VAL in TypeScript<\/h3>\n<p>In TypeScript, MIN_VAL and MAX_VAL are constants that represent the minimum and maximum values that can be stored in a number data type. These constants can be used to validate input or to define numeric boundaries in your code.<\/p>\n<p>For example, you might use the MIN_VAL and MAX_VAL constants to validate that a user-entered number is within a certain range:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const DataFlair_MIN_VAL = -100;\r\nconst DataFlair_MAX_VAL = 100;\r\n\r\nfunction validateInput(input: number): boolean {\r\n return input &gt;= DataFlair_MIN_VAL &amp;&amp; input &lt;= DataFlair_MAX_VAL;\r\n}\r\n\r\nconsole.log(validateInput(50)); \/\/ true\r\nconsole.log(validateInput(200)); \/\/ false\r\n<\/pre>\n<p><strong>Output &#8211;<\/strong><\/p>\n<div class=\"code-output\">true<br \/>\nfalse<\/div>\n<p>In addition to MIN_VAL and MAX_VAL, TypeScript also provides two special constants for representing positive and negative infinity. These constants are POSITIVE_INFINITY and NEGATIVE_INFINITY, respectively.<\/p>\n<p>POSITIVE_INFINITY and NEGATIVE_INFINITY are constants that represent positive and negative infinity, respectively. These values are returned when a mathematical operation exceeds a number&#8217;s maximum or minimum representable value.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">console.log(1 \/ 0); \/\/ Infinity\r\nconsole.log(-1 \/ 0); \/\/ -Infinity\r\n<\/pre>\n<p><strong>Output<\/strong> &#8211;<\/p>\n<div class=\"code-output\">Infinity<br \/>\n-Infinity<\/div>\n<p>It is important to know these constants when working with numerical data in TypeScript to ensure that your code handles edge cases and potential overflow errors correctly.<\/p>\n<h3>Type Conversions in TypeScript<\/h3>\n<p>TypeScript also provides a way to convert between different numeric types. For example, you can convert a number to a bigint using the BigInt() function. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_x: number = 42;\r\nlet DataFlair_big: bigint = BigInt(x);\r\n<\/pre>\n<p>Similarly, you can convert a number to a string using the toString() method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let DataFlair_x: number = 42;\r\nlet DataFlair_big: string = DataFlair_x.toString();\r\n<\/pre>\n<h3>Number Methods in TypeScript<\/h3>\n<p>TypeScript provides several built-in methods for working with numerical data. Some of the most commonly used methods include:<\/p>\n<p><strong>1. toFixed(n: number): string:<\/strong> Returns a string representation of the number with n decimal places.<\/p>\n<p><strong>2. toPrecision(n: number): string:<\/strong> Returns a string representation of the number with n significant figures.<\/p>\n<p><strong>3. toString(radix?: number): string:<\/strong> Returns a string representation of the number in the specified base (radix). The default is base 10.<\/p>\n<p><strong>4. isNaN(): boolean:<\/strong> Returns true if the number is NaN (not a number), false otherwise.<\/p>\n<p><strong>5. isFinite(): boolean:<\/strong> Returns true if the number is finite (i.e., not Infinity, -Infinity, or NaN), false otherwise.<\/p>\n<p><strong>6. parseFloat(str: string): number:<\/strong> Parses a string and returns the floating-point number it represents.<\/p>\n<p><strong>7. parseInt(str: string, radix?: number):<\/strong> number: Parses a string and returns the integer it represents in the specified base (radix). The default is base 10.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const DataFlair_num = 3.14159;\r\n\r\n\r\nconsole.log(DataFlair_num.toFixed(2)); \/\/ \"3.14\"\r\nconsole.log(DataFlair_num.toPrecision(3)); \/\/ \"3.14\"\r\nconsole.log(DataFlair_num.toString(16)); \/\/ \"3.243f6a8885a3\"\r\nconsole.log(isNaN(DataFlair_num)); \/\/ false\r\nconsole.log(isFinite(DataFlair_num)); \/\/ true\r\nconsole.log(parseFloat('3.14159')); \/\/ 3.14159\r\nconsole.log(parseInt('1010', 2)); \/\/ 10\r\n<\/pre>\n<p><strong>Output<\/strong>&#8211;<\/p>\n<div class=\"code-output\">3.14<br \/>\n3.14<br \/>\n3.243f3e037<br \/>\nfalse<br \/>\ntrue<br \/>\n3.14159<br \/>\n10<\/div>\n<p>These methods provide a convenient way to work with numerical data in TypeScript and can be used to perform a wide range of mathematical operations and conversions.<\/p>\n<h3>Conclusion<\/h3>\n<p>Strong tools for working with numbers are provided by TypeScript, such as support for diverse numeric types, number literals, unique numeric values, and type conversions. By utilizing TypeScript&#8217;s robust type features, you may develop more dependable code and catch problems earlier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In TypeScript, numbers are a primitive data type used to represent numerical values. The number type can hold both integers and floating-point numbers. TypeScript supports basic arithmetic operations, such as addition, subtraction, multiplication, and&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":112986,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27319],"tags":[27348],"class_list":["post-112695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript-tutorials","tag-numbers-in-typescript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Numbers in TypeScript - DataFlair<\/title>\n<meta name=\"description\" content=\"In TypeScript, numbers are a primitive data type used to represent numerical values. Learn its basic types, number literals and methods etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numbers in TypeScript - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In TypeScript, numbers are a primitive data type used to represent numerical values. Learn its basic types, number literals and methods etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-05T22:18:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-06T10:18:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Numbers in TypeScript - DataFlair","description":"In TypeScript, numbers are a primitive data type used to represent numerical values. Learn its basic types, number literals and methods etc.","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:\/\/data-flair.training\/blogs\/numbers-in-typescript\/","og_locale":"en_US","og_type":"article","og_title":"Numbers in TypeScript - DataFlair","og_description":"In TypeScript, numbers are a primitive data type used to represent numerical values. Learn its basic types, number literals and methods etc.","og_url":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-05-05T22:18:27+00:00","article_modified_time":"2023-05-06T10:18:04+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Numbers in TypeScript","datePublished":"2023-05-05T22:18:27+00:00","dateModified":"2023-05-06T10:18:04+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/"},"wordCount":760,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp","keywords":["Numbers in TypeScript"],"articleSection":["TypeScript Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/","url":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/","name":"Numbers in TypeScript - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp","datePublished":"2023-05-05T22:18:27+00:00","dateModified":"2023-05-06T10:18:04+00:00","description":"In TypeScript, numbers are a primitive data type used to represent numerical values. Learn its basic types, number literals and methods etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/typescript-numbers.webp","width":1200,"height":628,"caption":"typescript numbers"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numbers-in-typescript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"TypeScript Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/typescript-tutorials\/"},{"@type":"ListItem","position":3,"name":"Numbers in TypeScript"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/112695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=112695"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/112695\/revisions"}],"predecessor-version":[{"id":112988,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/112695\/revisions\/112988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/112986"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=112695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=112695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=112695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}