Skip to content
Related Articles

Related Articles

Improve Article

D3.js format() Function

  • Last Updated : 23 Jul, 2020

The format() function in D3.js is used to format the numbers in different styles available in D3. It is the alias for locale.format.



Parameters: It takes the parameters given above and described below.

  • specifier: It specifies the format type.
  • value: It is the number to be formatted.

Return Value: It returns the Number.

Below given are a few examples of the above function.


<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport"
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "">
    // It gives the floating point up to two decimals
    // It sets the SI-Prefix with two significant digits
    // Currency sign i.e $
    console.log(d3.format("$, ")(4200))
    /* Left and right side of the number is
        filled with dots and centered.*/
    console.log(d3.format("^, ")(42.444))
    // Localised fixed-point currency
    // Prefixed lowercase hexadecimal number
    // Prefixed lowercase hexadecimal number
    /* Number is grouped thousand with 
      two significant digits*/
    console.log(d3.format(", .2r")(42124))


My Personal Notes arrow_drop_up
Recommended Articles
Page :