TypeScript Numbers

Numbers are used to perform mathematical operation. All numbers are stored as floating point numbers. Number literals can form of binary, octal, decimal, hexadecimal and floating point values.

The binary number starts with 0b and consists of 0s and 1s. For example, 0b1100 is a binary representation and its decimal value is 12.

let value : number = 0b1100; // Binary literal    
// Display stored value
console.log(value); // 12

Observe that by default binary literals are stored in decimal format. In the case when the binary value needs to be displayed, we can use the toString() method.

let value : number = 0b1100; // Binary literal    

// Display stored value
console.log(value); // 12

// Display binary value
console.log(value.toString(2)); // 1100

Here the base 2 value is given in the toString() method. These 12 decimal values represent binary formats (1100).

Octal Number : A decimal number which is start with 0o it is called octal number.

let value : number = 0o12; // Octal literal   

// Display stored value
console.log(value); // 10

// Display Octal value
console.log(value.toString(8)); // 12

Decimal Number : is positive and negative numbers which contain digit of [0-9]. And large number can be separated by underscore.

let a : number = 123; 
let b : number = 99_9_99; 

// Display stored value
console.log(a); // 123
console.log(b); // 99999

Hexadecimal Number : This number are start with 0x. The suffix may contain numerals such as [0-9] and alphabet [A-F (A-F)] letters.

let value : number = 0x7b; // Hexadecimal literal 

// Display stored value
console.log(value); // 123

// Display Hexadecimal value
console.log(value.toString(16)); // 7b

Number Methods

Method Overview
toExponential() Returns the exponential notation in string format.
toFixed() Returns the fixed-point notation in string format.
toLocaleString() Converts the number into a local specific representation of the number.
toPrecision() Returns the string representation in exponential or fixed-point to the specified precision.
toString() Returns the string representation of the number in the specified base.
valueOf() Returns the primitive value of the number.

toExponential()

let num : number = 102334234;
// Test
console.log(num.toExponential());
console.log(num.toExponential(3));
console.log(num.toExponential(2));
console.log(num.toExponential(1));
1.02334234e+8
1.023e+8
1.02e+8
1.0e+8

toFixed()

let num : number = 102.934234;
// Test
console.log(num.toFixed());
console.log(num.toFixed(3));
console.log(num.toFixed(2));
console.log(num.toFixed(1));
103
102.934
102.93
102.9

toLocaleString()

Options Overview
ar-SA Arabic (Saudi Arabia)
bn-BD Bangla (Bangladesh)
bn-IN Bangla (India)
cs-CZ Czech (Czech Republic)
da-DK Danish (Denmark)
de-AT Austrian German
de-CH "Swiss" German
de-DE Standard German (Germany)
el-GR Modern Greek
en-AU Australian English
en-CA Canadian English
en-GB British English
en-IE Irish English
en-IN Indian English
en-NZ New Zealand English
en-US US English
en-ZA English (South Africa)
es-AR Argentine Spanish
es-CL Chilean Spanish
es-CO Colombian Spanish
es-ES Castilian Spanish (as spoken in Central-Northern Spain)
es-MX Mexican Spanish
es-US American Spanish
fi-FI Finnish (Finland)
fr-BE Belgian French
fr-CA Canadian French
fr-CH "Swiss" French
fr-FR Standard French (France)
he-IL Hebrew (Israel)
hi-IN Hindi (India)
hu-HU Hungarian (Hungary)
id-ID Indonesian (Indonesia)
it-CH "Swiss" Italian
it-IT Standard Italian (Italy)
ja-JP Japanese (Japan)
ko-KR Korean (Republic of Korea)
nl-BE Belgian Dutch
nl-NL Standard Dutch (Netherlands)
no-NO Norwegian (Norway)
pl-PL Polish (Poland)
pt-BR Brazilian Portuguese
pt-PT European Portuguese (Portugal)
ro-RO Romanian (Romania)
ru-RU Russian (Russian Federation)
sk-SK Slovak (Slovakia)
sv-SE Swedish (Sweden)
ta-IN Indian Tamil
ta-LK Sri Lankan Tamil
th-TH Thai (Thailand)
tr-TR Turkish (Turkey)
zh-CN Mainland China, simplified characters
zh-HK Hong Kong, traditional characters
zh-TW Taiwan, traditional characters
var num : number = 345.9875643;
// Test
console.log(num.toLocaleString());
console.log(num.toLocaleString('ja-JP'));
console.log(num.toLocaleString('de-AT'));
console.log(num.toLocaleString('tr-TR'));
console.log(num.toLocaleString('fr-FR'));
"34,434,555.988"
"34,434,555.988" 
"34 434 555,988" 
"34.434.555,988" 
"34 434 555,988"

toPrecision()

var num : number = 345.9875643;
// Test
console.log(num.toPrecision());
console.log(num.toPrecision(4));
console.log(num.toPrecision(3));
console.log(num.toPrecision(2));
console.log(num.toPrecision(1));
345.9875643
346.0
346
3.5e+2
3e+2

toString()

let num : number = 345.9875643;

let strNum : string = num.toString();
// Test
console.log(num,typeof num);
console.log(strNum,typeof strNum);
345.9875643 'number'
345.9875643 string
let num : number = 123;

// test
console.log(num.toString(2)); // Binary
console.log(num.toString(16)); // Hex number
console.log(num.toString(8)); // Octal
console.log(num.toString(4)); // Base 4
1111011
7b
173
1323

valueOf()

let num  = new Number(143);

// test
console.log(typeof num);   // number
console.log(num.valueOf()); // 143 


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved