Integer types in TypeScript
In TypeScript , there is no concept of integer data types like in other programming languages. Usually, only number
type is used to represent floating point numbers.
bigint
is the latest version of TypeScript, which represents large integers.
number
Representing numbers
in TypeScript using the type
The type in TypeScript number
can hold both integer and floating point data types.
It can also store numbers in different bases such as binary, octal, and hexadecimal.
// the count of something is in whole numbers
var countDogs : number = 10;
// the price of a bag can be a floating point number
var priceBag : number = 200.99;
var binaryNumber : number = 0b101;
var octalNumber : number = 0o30;
var hexadecimalNumber : number = 0xFF;
console.log(countDogs);
console.log(priceBag);
console.log(binaryNumber);
console.log(octalNumber);
console.log(hexadecimalNumber);
Output:
10
200.99
5
24
255
Numbers in different bases are base 10
printed with . Binary or base 2
can be represented by prefixing 0b
or 0B
, similarly for base 16
, it must be prefixed with 0x
or .0X
In TypeScript, we bigint
use
bigint
The type is used to represent very large numbers (numbers more than 2<sup>53</sup> - 1)
and has the character at the end of integer literals n
.
var bigNumber: bigint = 82937289372323n;
console.log(bigNumber);
Output:
82937289372323
parseInt
Convert a string to an integer
using built-in functions in TypeScript
parseInt
Function is used to convert from string to integer and parseFloat
function is used to convert from string to floating point data type.
var intString : string = "34";
var floatString : string = "34.56";
console.log(parseInt(intString));
console.log(parseInt(floatString));
console.log(parseFloat(intString));
console.log(parseFloat(floatString));
var notANumber : string = "string";
console.log(parseInt(notANumber));
Output:
34
34
34
34.56
NaN
Therefore parseInt
the and parseFloat
methods will return NaN
.
+
Convert a string to a number
using the operator in TypeScript
+
Operator can convert a string literal to number
type. After converting it to number
type, further operations are performed.
function checkiFInt( val : number | string ) {
if (( val as any) instanceof String){
val = +val as number;
}
console.log(Math.ceil(val as number) == Math.floor(val as number));
}
checkiFInt('34.5');
checkiFInt('34');
checkiFInt('34.5232');
checkiFInt('34.9');
checkiFInt('0');
Output:
false
true
false
false
true
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Handling exceptions with try..catch..finally in TypeScript
Publish Date:2025/04/15 Views:170 Category:TypeScript
-
This article will discuss try..catch..finally handling exceptions in TypeScript using the statement. Handling exceptions in TypeScript In TypeScript, try..catch..finally a block handles exceptions that occur during program runtime. It allow
Convert JSON object to a class in TypeScript
Publish Date:2025/04/15 Views:80 Category:TypeScript
-
Data on the internet flows in the form of strings, which can be converted into a very popular format called JSON. The JSON representation of this data usually represents an object or even a class in TypeScript. TypeScript provides the abili
Update TypeScript to the latest version using NPM
Publish Date:2025/04/15 Views:119 Category:TypeScript
-
This article provides a guide to update to the latest version of TypeScript using npm, which is the best and most popular method used by developers. This also gives us an in-depth look at what npm is and why to use it. Node Package Manager
Using jQuery and TypeScript
Publish Date:2025/04/15 Views:148 Category:TypeScript
-
This article provides the basic understanding and concepts of using jQuery with TypeScript. It guides on how to use jQuery in TypeScript through a coding example and outputs using various methods in TypeScript and provides knowledge about w
Checking for undefined in TypeScript
Publish Date:2025/04/15 Views:196 Category:TypeScript
-
This article will demonstrate how programmers can check for undefined in TypeScript using various coding examples and situations. It not only gives you an understanding of checking for undefined in TypeScript, but also helps in differentiat
exclude attribute in TypeScript
Publish Date:2025/04/15 Views:190 Category:TypeScript
-
This article will discuss excluding properties from a TypeScript type. The exclude property is used to create another type from an existing type with fewer or modified properties. Omit Exclude properties from types in TypeScript TypeScript
Function return types in TypeScript
Publish Date:2025/04/15 Views:174 Category:TypeScript
-
TypeScript is a strongly typed language and it is always encouraged to define the types correctly for all variables used in TypeScript. These types can help in development later, especially during debugging. In addition to variables, functi
Dictionary or map type in TypeScript
Publish Date:2025/04/15 Views:131 Category:TypeScript
-
Dictionaries or maps are used to quickly retrieve items from an object. TypeScript does not have any concept of maps or dictionaries. Plain JavaScript has objects that can set and retrieve key-value pairs. TypeScript provides Record types t
Regular expressions in TypeScript
Publish Date:2025/04/15 Views:104 Category:TypeScript
-
RegExp Is a regular expression used to match a fixed set of characters in a target string using TypeScript. RegExp Objects have many properties and methods. Each property has different options when pattern matching. Using regular expression