JavaScript. Functions for representing the Number object in various notations

Functions for representing the Number object in various notations. Functions toExponential(), toFixed(), toLocaleString(), toPrecision(), toString(), valueOf()


Contents


Search other resources:

1. An overview of the functions of representing the Number object

The Number object has a number of functions to represent it in different forms (notations):

  • toExponential();
  • toFixed();
  • toLocaleString();
  • toPrecision();
  • toString();
  • valueOf().

 

2. Function Number.toExponential(). Get string with exponential form of number

The toExponential() function allows you to get a string in exponential notation (form). The function contains several variants of declarations

value.toExponential()
value.toExponential(frac_digits)

here

  • value – numeric value of the Number type to be represented in exponential form;
  • frac_digits is an integer value specifying the number of digits after the decimal point. If the frac_digits argument is omitted, then as many digits are specified as needed to uniquely represent value. The frac_digits value must be between 0 and 100 inclusive. If you set a frac_digits value that is outside these limits, a RangeError exception will be thrown.

Characteristic features of using the method:

  • if you call a method on an object that is not a Number object, then a TypeError exception will be thrown;
  • if you need to use the method for a numeric literal that does not have a decimal point, then a space character ‘ ‘ is placed before the point;
  • if the number contains more digits than specified in the frac_digit argument, then this number is rounded up to the nearest number. The number of digits in this closest number is frac_digit.

Example.

// Function toExponential() - get number in exponential notation

// 1. Using a function without parameters
var a = 5/6
var res
res = a.toExponential() // res = 8.333333333333334e-1

res = 1234.235.toExponential() // res = 1.234235e+3
res = Number(228/11).toExponential() // res = 2.0727272727272727e+1
res = Number(345678).toExponential() // res = 3.45678e+5

// 2. Using the function with the precision parameter
res = 1234.56789.toExponential(3) // res = 1.235e+3
res = 1234.56789.toExponential(1) // res = 1.2e+3
res = (10000/3).toExponential(0) // res = 3e+3

// 3. Specified with a 'space' symbol for correct interpretation
res = 100 .toExponential() // 1e+2

 

3. Function Number.toFixed(). Get a number using fixed point notation

The toFixed() function allows you to get the string representation of a number with a specified number of decimal places. The function has two forms of use

value.toFixed()
value.toFixed(digits)

here

  • value – numeric value of the Number type, which must be represented with a fixed precision;
  • digits – number of decimal places to be received (displayed). The value of digits must be between 0 and 20 inclusive. If the digits argument is not provided, it is treated as 0. If the value of digits is outside [0; 20], then a RangeError exception is thrown.

Features of using the function:

  • if you call the function for a value that is not of type Number, then a TypeError exception will be generated;
  • the function does not return the exponential representation of the Number object;
  • if the number of decimal digits after the decimal point in Number is greater than the digits value, then the result is rounded off;
  • if the number of decimal digits after the decimal point in Number is less than the digits value, then the result is padded with zeros;
  • if valueі≥e+21, then the Number.prototype.toString() function is called and the toFixed() function returns the exponential representation of the number.

Example.

// Function toFixed() - get a number with specified precision

// 1. Using a function with 1 parameter
var a = 5/6
var res = Number.parseFloat(a).toFixed(2) // res = 0.83

a = 1/3
res = a.toFixed(4) // res = 0.3333

res = 0.2.toFixed(5) // res = 0.20000
res = Number(2.830239).toFixed(0) // res = 3
res = Number.parseFloat(-1.88).toFixed(8) // res = -1.88000000

// 2. Using function without parameters
a = 15.203367
res = a.toFixed() // res = 15
res = 109.209.toFixed() // res = 109

// 3. Specifying a value greater than 1e+21
a = 1e+21 + 1
res = a.toFixed() // res = 1e+21
res = a.toFixed(7) // res = 1e+21

 

4. Function Number.toLocaleString(). Get a string representing a number according to the given language

The Number.toLocaleString() function returns a string from the representation of this number in the given language. According to the documentation, the function has several use cases.

value.toLocaleString()
value.toLocaleString(locales)
value.toLocaleString(locales, options)

here

  • value – the value being processed;
  • locales – sets the language according to which the resulting string is determined;
  • options – an argument that forms the details of the representation of the locale. The options argument is given as key:value pairs enclosed in curly braces { } (see example below). For more information on using the options argument, see the JavaScript documentation.

If the function is called without parameters, the form of the resulting string is entirely implementation dependent.

Example.

// Function toLocaleString()

// 1. Using a function without parameters

var a = 2345
var res = a.toLocaleString() // res = "2,345" - locale U.S. English

// 2. Using a function with 1 parameter toLocaleString(locales)

// 2.1. Checking if a given locale is being used in a browser
a = 1282
try {
  res = a.toLocaleString('en-IN') // res = "1,282"
}
catch (e) {
  res = e.name("RangeError")
}

// 2.2. Specifying the locale format without checking
res = a.toLocaleString("uk-UA") // res = "1 282"
res = a.toLocaleString("ru-RU") // res = "1 282"
res = a.toLocaleString("en-US") // res = "1,282"

// 3. Using a function with 2 parameters
//   toLocaleString(locales, options)

// 3.1. Set the US currency format for the specified number
a = 28783.59
var opt = { style : 'currency', currency : 'USD' } // Form options
res = a.toLocaleString("en-US", opt) // res = "$28,783.59"

// 3.2. Set scientific format
opt = { notation : 'scientific'} // Form options
res = a.toLocaleString("en-GB", opt) // res = "2.878E4"

// 3.3. Set the digital system 'arabext'
opt = { numberingSystem : 'arabext' }
res = a.toLocaleString("ar-EN", opt) // res = "۲۸٬۷۸۳٫۵۹"

 

5. Function Number.toPrecision(). Get a string based on a number with a given precision

The toPrecision() function returns a string representing a Number object with the given precision. This function can return a string in fixed notation and exponential notation.

The function has two call cases

value.toPrecision()
value.toPrecision(precision)

here

  • value – the object Number;
  • precision – an integer value specifying the number of significant digits. The value of precision must be between 1 and 100 inclusive. If the precision value is set outside the range [1; 100], a RangeError exception will be thrown.

Features of the function call:

  • if the function is called without parameters (without the precision argument), this function works the same as Number.toString();
  • if the precision argument is not an integer, it is rounded to the nearest integer.

Example.

// Function toPrecision()

// 1. Using a function without parameters
var a = 12345.6789
var res = a.toPrecision() // res = "12345.6789"

// 2. Using a function with a precision argument
a = 1234.56789
res = a.toPrecision(1) // res = "1e+3"
res = a.toPrecision(2) // res = "1.2e+3"
res = a.toPrecision(3) // res = "1.23e+3"
res = a.toPrecision(4) // res = "1235"
res = a.toPrecision(5) // res = "1234.6"
res = a.toPrecision(6) // res = "1234.57"
res = a.toPrecision(7) // res = "1234.568"
res = a.toPrecision(8) // res = "1234.5679"
res = a.toPrecision(9) // res = "1234.56789"
res = a.toPrecision(10) // res = "1234.567890"
res = a.toPrecision(11) // res = "1234.5678900"

 

6. Function Number.toString(). Get the string representation of a number in the specified number system

The toString() method returns a string representing the given Number object in the specified number system. The method can be used in two forms

value.toString()
value.toString(radix)

here

  • value – a Number object that is converted to a string;
  • radix –radix base number system. The radix value can be from 2 to 36.

Features of the function:

  • if the radix value is outside [2; 36], then a RangeError exception is thrown;
  • this function (method) toString() redefines the method of the same name of the Object object;
  • if radix≥10, then subsequent digits following the digit ‘9’ are used with letters of the Latin alphabet from ‘a’ to ‘z’;
  • if the number system is not specified, then it is considered that the number system with base 10 is desirable.

Example.

// Function toString()

// 1. Using a function without parameters
var a = 2505
var res = a.toString() // res = 2505

// 2. Using a function with a radix() argument
a = 255
res = a.toString(16) // res = 'ff' - hexadecimal number system

a = 10
res = a.toString(2) // res = "1010" - binary numeral system

a = 100
res = a.toString(8) // res = '144' - octal number system

 

7. Function Number.valueOf(). Get an object based on a given value

The Number.valueOf() method returns a primitive value wrapped in a Number object. The use of the function could be like this

value.valueOf()

Here value is a primitive value that is wrapped by an object of type Number.

Example.

// Function valueOf()

var res = Number(255).valueOf() // res = 255
res = "333".valueOf() // res = 333

var num = 25.88
res = num.valueOf() // res = 25.88

var str = "abcd"
res = str.valueOf() // res = "abcd"

var b = true
res = b.valueOf() // res = true

 


Related topics