D3.js中的format()函數用於格式化D3中可用的不同樣式的數字。它是locale.format的別名。
用法:
d3.format(specifier)(value);
參數:它采用上麵給定和下麵描述的參數。
- specifier:它指定格式類型。
- value:這是要格式化的數字。
返回值:它返回數字。
下麵給出了上述函數的一些示例。
範例1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<style>
</style>
<body>
<!--Fetching from CDN of D3.js -->
<script type = "text/javascript"
src = "https://d3js.org/d3.v4.min.js">
</script>
<script>
// It gives the floating point up to two decimals
console.log(d3.format(".2f")(42.444))
// It sets the SI-Prefix with two significant digits
console.log(d3.format(".4s")(42000))
// 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
console.log(d3.format("($.2f")(42.444))
console.log(d3.format(".^20")(42))
// Prefixed lowercase hexadecimal number
console.log(d3.format("#x")(2))
// Prefixed lowercase hexadecimal number
console.log(d3.format("#x")(42))
/* Number is grouped thousand with
two significant digits*/
console.log(d3.format(", .2r")(42124))
</script>
</body>
</html>
輸出:
相關用法
- PHP DateTime format()用法及代碼示例
- PHP IntlDateFormatter format()用法及代碼示例
- Express.js res.format()用法及代碼示例
- Moment.js moment().format()用法及代碼示例
- Node.js util.format()用法及代碼示例
- Node.js path.format()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js format() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。