ylbtech-JavaScript-Tool:Numeral.js

A javascript library for formatting and manipulating numbers.

1. # Use it返回顶部
It the Brower
<script src="numeral.min.js"></script>

or incloude from cndjs.com
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

In Node.js
npm install numeral

var numeral = require('numeral');

2. # Create返回顶部
Create an instance of a numeral. Numeral takes numbers or strings that it trys to convert into a number.
var myNumeral = numeral(1000);var value = myNumeral.value();
// 1000var myNumeral2 = numeral('1,000');var value2 = myNumeral2.value();
// 1000

3. # Format返回顶部
Create an instance of a numeral. Numeral takes numbers or strings that it trys to convert into a number. 
Numbers can be formatted to look like currency, percentages, times, or even plain old numbers with decimal places, thousands, and abbreviations. And you can always  create a custom format.
var string = numeral(1000).format('0,0');
// '1,000'

Numbers

Currency

Bytes

Percentages

Time

Exponential

4. # Founctions返回顶部

Value

The value is always available.

var number = numeral(1000);var string = number.format('0,0');
// '1,000'var value = number.value();
// 1000

Manipulate

Not that you will use these often, but they're there when you need them.

var number = numeral(1000);var added = number.add(10);
// 1010

Set

Set the value of your numeral object.

var number = numeral();number.set(1000);var value = number.value();
// 1000

Difference

Find the difference between your numeral object and a value

var number = numeral(1000),value = 100;var difference = number.difference(value);
// 900

Clone

Go ahead and clone any numeral object while you're at it.

var a = numeral(1000);
var b = numeral(a);
var c = a.clone();var aVal = a.set(2000).value();
// 2000var bVal = b.value();
// 1000var cVal = c.add(10).value();
// 1010

5. # Settings返回顶部

Default Formatting

Set a default format so you can use .format() without a string. The default format to '0,0'

var number = numeral(1000);number.format();
// '1,000'

numeral.defaultFormat('$0,0.00');number.format();
// '$1,000.00'

Custom Zero and Null Formatting

Set a custom output when formatting numerals with a value of 0 or null

var number = numeral(0);
var nullNumber = numeral(null);numeral.zeroFormat('N/A');
numeral.nullFormat('N/A');var zero = number.format('0.0')
// 'N/A'var na = nullNumber.format('0.0')
// 'N/A'

6. Locales返回顶部

Let's make this useable all over the place!

// load a locale
numeral.register('locale', 'fr', {delimiters: {thousands: ' ',decimal: ','},abbreviations: {thousand: 'k',million: 'm',billion: 'b',trillion: 't'},ordinal : function (number) {return number === 1 ? 'er' : 'ème';},currency: {symbol: '€'}
});// switch between locales
numeral.locale('fr');

As I am not fluent in every locale on the planet, please feel free to create locale files of your own by submitting a pull request. Don't forget to create both the locale file (example: locales/fr.js) and the locale test (example: tests/locales/fr.js). Thanks for helping out.

7. # Fomates返回顶部

Adding your own custom formats is as easy as adding a locale.

// load a format
numeral.register('format', 'percentage', {regexps: {format: /(%)/,unformat: /(%)/},format: function(value, format, roundingFunction) {var space = numeral._.includes(format, ' %') ? ' ' : '',output;value = value * 100;// check for space before %format = format.replace(/\s?\%/, '');output = numeral._.numberToFormat(value, format, roundingFunction);if (numeral._.includes(output, ')')) {output = output.split('');output.splice(-1, 0, space + '%');output = output.join('');} else {output = output + space + '%';}return output;},unformat: function(string) {return numeral._.stringToNumber(string) * 0.01;}
});// use your custom format
numeral().format('0%');

8. # Acknowlegements返回顶部
Numeral.js, while less complex, was inspired by and heavily borrowed from  Moment.js
9.返回顶部
10.返回顶部
1、官网
http://numeraljs.com/
2、GitHub
https://github.com/adamwdraper/Numeral-js
3、adamwdraper
https://github.com/adamwdraper
4、
11.返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/storebook/p/8352491.html

JavaScript-Tool:Numeral.js相关推荐

  1. JavaScript奇技淫巧:加密JS代码反调试

    JavaScript奇技淫巧:加密JS代码反调试 JS代码混淆加密,已被很多人使用,因为它真的很有用.很实用,可以用于保护代码.防护分析.复制.盗用,还可以用于小游戏过审.APP加固等方面. 混淆加密 ...

  2. 数字格式化工具:Numeral.js 简介

    2019独角兽企业重金招聘Python工程师标准>>> Numeral.js 是一个用于格式化和操作数字的 JavaScript 库.数字可以格式化为货币,百分比,时间,甚至是小数, ...

  3. Javascript模块化编程(三):require.js的用法

    这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...

  4. JavaScript:引用js文件时的编码格式问题

    今天在jsp页面引入js时,网页查看源码js文件老是乱码,弄了半天,终于解决了. 如果js文件的编码格式是utf-8,并且含有中文,那么按照正常的方法引用,就会出现乱码的情况. 方法/步骤 如果js文 ...

  5. 使用HTML5画布实现的超棒javascript动画仪表板:gauge.js

    日期:2012-8-8  来源:GBin1.com 在线演示 今天我们分享来自guage.js的超棒动画仪表板实现,这个类库使用html5画布来生成动态的自定义仪表板.不依赖于任何类库也不实用任何的C ...

  6. JavaScript奇淫技巧:把JS编译成exe

    JavaScript奇淫技巧:把JS编译成exe 本文,介绍一种一般人不知道的技术:把JS代码编译成exe,而且不使用第三方工具,仅用系统工具完成. 第一:工具准备 编译JS代码,使用一个名为jsc. ...

  7. 自学JavaScript第四天- JS 进阶:AJAX Promise Canvas

    自学JavaScript第四天- JS 进阶:AJAX Promise Canvas AJAX 使用 XMLHttpRequest 使用 fetch() 方法 处理 AJAX 数据 安全限制 跨域方案 ...

  8. 项目问题记录:使用javascript打字库type.js时出现两个光标——useeffect销毁元素

    今天项目遇到一个问题,哎好宝,还改了很久- 引用type.js,这是一个模拟打字的js库,具体可以参照官方文档: github官方文档 这个博客写了操作,比较详尽: type.js--javascri ...

  9. Eloquent JavaScript 笔记 十九:Node.js

    1. Background 可以略过. 2. Asynchronicity 讲同步和异步的基本原理,可以略过. 3. The Node Command 首先,访问 nodejs.org 网站,安装no ...

最新文章

  1. python函数拟合不规则曲线_python中的多变量(多项式)最佳拟合曲线?
  2. 微信企业号 JS-SDK:上传图片
  3. 将ALV GRID中改变的值更新到ALV内表中
  4. 安装Ubuntu 20.04.1 LTS (Focal Fossa)到U盘并安装ros2
  5. MyEclipse/Eclipse快捷键调试大全
  6. php 可以动态的new一个变量类名
  7. javascript淘宝主图放大镜功能
  8. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
  9. java shp文件_Java读取工作空间下所有shp文件名
  10. 设计模式之不简单的工厂模式(二)
  11. css3漂亮的渐变图案,CSS3 带渐变图案的圆球
  12. [ZT]CCNA课堂精简笔记 2
  13. 阶段3 2.Spring_01.Spring框架简介_03.spring概述
  14. 关系数据库设计理论--3NF
  15. 全外显子组生信分析流程-4-数据质控
  16. win10 更新1803版本后删除自带微软拼音的方法
  17. matlab处理各种数据、文件
  18. 25.(cesium篇)cesium军事标绘-攻击箭头采集(燕尾)
  19. xx-xx-xx-xx转换成x年x月x日星期x
  20. 学习网络攻防,有什么渠道?

热门文章

  1. fluent物性参数拟合多项式,python,matlab多项式图像绘制
  2. 解决GitHub/GitLab官网访问慢的问题
  3. JSONObject.toBean() 把jsonobject转换成实体类
  4. java的mysql语句规范_mysql 规范
  5. SQL Server中,备份数据表
  6. 哈工大c语言ex6答案,哈工大C语言程序设计习题ex6.doc
  7. Js与Jq实战:第七讲:jQuery基础
  8. 【记录】在win10系统上安装ubuntu(乌班图)双系统
  9. matlab 绘制高斯(Gaussan)函数图像
  10. Bus error (core dumped) 我重启了下superviser 资源cpu占用高