Warning: session_start(): open(/tmp/sess_238b5a75bafa9bbfa635eb2b2525ec3a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
TypeScript Numbers: How To Create And Work?

How To Create And Work With TypeScript Numbers

How To Create And Work With TypeScript Numbers

TypeScript keeps the same primitive data type named number that JavaScript provides. This means you will encounter similar features of this type if you are switching from JavaScript.

Read on to have a more detailed look at TypeScript numbers.

TypeScript Numbers

Traditionally, TypeScript doesn’t have separate data types for different numeric data types like integers and floating-point numbers (with bigint as the only exception). It inherits this design from JavaScript, which is a major departure from other languages like C++ or Java.

The set of numeric values in the number type includes integers, floating-point numbers, NaN, and Infinity.

bigint is a new primitive (just added in ES2020) that represents very large integers. You will need TypeScript 3.2 or newer to support this type.

Numeric Literals

Numeric literals in TypeScript include integers in different bases and floating-point numbers in base 10.

Remember that at the end of the day, the runtime environment will treat them at floating-point values and use the IEEE–754 format to represent them since there is no separate type for integers. These representations are usually called double-precision floating-point numbers in other languages.

Integer Literals

The most basic literal is decimal integers, such as:

15
33930
1

To represent decimal literals, you must not give them a leading zero.

You can also use literals in other bases such as binary, octal, hexadecimal.

To create octal (base 8) literals, start the literal with a zero or a zero or an oO character. Other digits after this prefix must be from 0 to 7. Otherwise, TypeScript will interpret them as decimal numbers.

These are octal literals of the decimals in the previous example:

017 // octal for 15
0o102212 // octal for 33930
0O1 // octal for 1

Likewise, you can create hexadecimal (base 16) literals by starting the sequence of digits with 0x or 0X characters. You can use both lower-case and upper-case letters from a to f to write hexadecimal literals, such as:

0x11 // hexadecimal for 15
0X848A // hexadecimal for 33930
0x1 // hexadecimal for 1

Use leading 0b or 0B characters to indicate a binary literal:

0b1111 // binary for 15
0B1000010010001010 // binary for 
0b1 // binary for 1

Floating-point Literals

TypeScript accepts the decimal point (.), which is also the traditional syntax for representing real numbers, in floating-point literals.

They can have up to four parts: an unsigned decimal integer, a decimal point, another decimal integer, and an exponent.

Some simple floating-point literals are:

3.48233
1022.33334

If the only before the decimal point is a zero, you can omit it:

.2393032

Use the letter e or E to represent the exponent:

22.34e+22
.12E-3

With long numeric literals, you can use underscores to break them up into smaller chunks. This way, your literals will be easier to read without altering the values they represent:

3_000_000
1.345_393_393_484

bigint

You will need to end a digit sequence with the n character to get a bigint literal.

100n
283984n

Special Values

TypeScript accepts values like Infinity, -Infinity, and NaN. They also belong to the number type set, representing positive infinity, negative infinity, and “not a number” values, respectively.

Create Numbers In TypeScript

With The Assignment Operator

The easiest method is to use the assignment operator. Since TypeScript is a JavaScript superset, you can ignore the declaration of your data type and let the compiler infer it from your value.

These are valid assignments in TypeScript:

let a = 12;
let b = 10.5;

This works with bigint values too:

let c = 250n;

However, you may want to explicitly restrict your variable to only numbers:

let a1: number = 12;
let b1: number = 10.5;
let c1: bigint = 250n;

Any attempt to assign values of other types to the variables above will result in errors:

a = 'LearnShareIT';
a1 = 'LearnShareIT';

Output:

Type 'string' is not assignable to type 'number'.

With Number() And BigInt()

TypeScript also supports the Number() constructor, which is used with the new keyword to create a Number object.

let c = new Number('123');

In this example, c is an instance of the Number class, not a number primitive:

console.log(typeof c);
console.log(c);

Output:

object
[Number: 123]

Number() is more frequently used as a function instead. In this case, it converts the argument (such as a string) to a number primitive:

let c = Number('123');
console.log(typeof c);
console.log(c);

Output:

number
123

For bigint values, use the BigInt function:

let d = BigInt(123);

Arithmetic Operations

TypeScript numbers work with several arithmetic operators, such as addition (+), subtraction (-), and multiplication (*), among several other operators.

Example:

let a = 100;
let b = 3;
console.log(++a);
console.log(--a);
console.log(100 + 4);
console.log(100 - 4);
console.log(100 / 4);
console.log(100 % 4);
console.log(100 * 4);
console.log(100 ** 4);

Output:

101
100
104
96
25
0
400
100000000

Common Number Functions And Methods

parseInt()

This function converts a string argument to an integer. You can provide another argument to specify the base of that string:

let a = parseInt('100');
console.log(a);
console.log(typeof a);

Output:

100
number

parseFloat()

To return a floating-point number from a string, use the parseFloat() function. It has no other arguments.

let a = parseFloat('1.83484');
console.log(a);
console.log(typeof a);

Output:

1.83484
number

toString()

This method returns the string representation of a number:

let a = 100;
let b = a.toString();
console.log(b);
console.log(typeof b);

Output:

100
string

Math.abs()

The built-in Math object provides additional methods and properties for mathematical functions. It only works with Number, not BigInt objects.

This function will give you the absolute value of a number.

let a = -100;
let b = Math.abs(a);
console.log(b);

Output:

100

Math.round()

The round() function rounds a number to its nearest integer. It can round up or down, depending on your value.

console.log(Math.round(1.2));
console.log(Math.round(1.9));

Output:

1
2

Math. floor(), Math.ceil()

On the other hand, floor() and ceil() only round down or up, respectively, to the nearest integer.

console.log(Math.floor(1.2));
console.log(Math.ceil(1.2));
console.log(Math.floor(1.9));
console.log(Math.ceil(1.9));

Output:

1
2

Tutorials

Learn more about TS Numbers in the article below:

Summary

TypeScript numbers are based on the primitive types number and bigint as well as the Number class. Most of the time, you only need number primitives, which are supported by plenty of operations and functions.

Leave a Reply

Your email address will not be published. Required fields are marked *