博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript-Tool:Numeral.js
阅读量:5011 次
发布时间:2019-06-12

本文共 4121 字,大约阅读时间需要 13 分钟。

ylbtech-JavaScript-Tool:Numeral.js

A javascript library for formatting and manipulating numbers.

1. # Use it
It the Brower
or incloude from cndjs.com
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 
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 localenumeral.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 localesnumeral.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 formatnumeral.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 formatnumeral().format('0%');

 

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

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

你可能感兴趣的文章
html5——动画案例(时钟)
查看>>
调用Android系统“应用程序信息(Application Info)”界面
查看>>
ios中用drawRect方法绘图的时候设置颜色
查看>>
数据库中的外键和主键理解
查看>>
个人博客03
查看>>
Expression<Func<T,TResult>>和Func<T,TResult>
查看>>
文件缓存
查看>>
关于C语言中return的一些总结
查看>>
Codeforces Round #278 (Div. 2)
查看>>
51. N-Queens
查看>>
Linux 命令 - 文件搜索命令 locate
查看>>
[Python] Execute a Python Script
查看>>
[Angular 2] Using Promise to Http
查看>>
[Grunt] grunt.template
查看>>
Ubuntu最小化桌面快捷键Super+D不生效解决
查看>>
Cookie&Session会话跟踪技术
查看>>
UNIX环境高级编程 第17章 高级进程间通信
查看>>
ES的Zen发现机制
查看>>
【hibernate】1、Hibernate的一个注解 @Transient
查看>>
HihoCoder 1877 - Approximate Matching
查看>>