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();
// var myNumeral2 = numeral('1,000'); var value2 = myNumeral2.value();
//
Input Value
 
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

Number Format String

Currency

Number Format String

Bytes

Number Format String

Percentages

Number Format String

Time

Number Format String

Exponential

Number Format String
 
4. # Founctions返回顶部

Value

The value is always available.

var number = numeral(1000);

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

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);
//
Before Function After

Set

Set the value of your numeral object.

var number = numeral();

number.set(1000);

var value = number.value();
//

Difference

Find the difference between your numeral object and a value

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

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();
// var bVal = b.value();
// var cVal = c.add(10).value();
//
 
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、官网
2、GitHub
3、adamwdraper
4、
 
11.返回顶部
 
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

JavaScript-Tool:Numeral.js的更多相关文章

  1. 检测设备平台,操作系统,方向 Javascript 库:Device.js

    Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <html> 标签添加一些设备平台,操作系统,方向相关的 CSS class,这样就 ...

  2. javascript动画:velocity.js学习

    第二章:基础知识 一.velocity和jQuery: Velocity函数是独立于jQuery的,但两者可以结合使用.通常这么做的好处是可以利用jQuery的链式操作:当你先用jQuery选择了一个 ...

  3. JavaScript入门:004—JS凝视的写法和基本运算符

    JS的凝视JS中加凝视和寻常写C#代码是几乎相同的.有//和/* */这两种.单行凝视使用双斜杠比如. <script type="text/javascript"> ...

  4. JavaScript入门:006—JS函数的定义

    JS函数的声明. 声明函数的格式例如以下: function 函数名(參数列表){ //函数语句: return 返回值; } 来看详细的函数声明.1.普通函数 <script type=&qu ...

  5. OpenPGP协议的一个JavaScript实现:OpenPGP.js

    OpenPGP.js 是OpenPGP协议的一个Javascript实现. 基于 JavaScript的OpenPGP实现方便用户可以直接在浏览器中加密和解密Web邮件,不需要专门的邮件客户端.

  6. JavaScript入门:002—JS代码放置的位置

    JavaScript在页面中使用,那么这些JS代码应该放在什么位置呢?以下来看一下. 一般来说有两种方式.写在界面上和使用.js文件.1.1界面上的Head部分能够直接放在head标签内,例如以下代码 ...

  7. JavaScript入门:003—JS中的变量

    编程语言都是同样的,JS中也是有变量的.首先JS的变量是区分大写和小写的,这个须要注意.比方number和Number是不同的变量.无论是经常使用类型的,还是对象类型,比方 Object obj和Ob ...

  8. 一个必用的javascript框架:underscore.js - wine的思考 - ITeye技术网站

    AngularJS+JqueryMobile+PhoneGap 打造APP « Dogeek AngularJS+JqueryMobile+PhoneGap 打造APP

  9. Numeral.js – 格式化和操作数字的 JavaScript 库

    Numeral.js 是一个用于格式化和操作数字的 JavaScript 库.数字可以格式化为货币,百分比,时间,甚至是小数,千位,和缩写格式,功能十分强大.支持包括中文在内的17种语言. 您可能感兴 ...

随机推荐

  1. Nutch相关视频教程3

    参考: http://www.cnblogs.com/huligong1234/p/3515214.html

  2. git 添加review的相关操作

    git主机上涉及到的操作: 185 git branch 186 git pull 187 git branch 188 git checkout -b wangcheng 189 git log 1 ...

  3. 玩转X-CTR100 l STM32F4 l WS2812全彩LED灯

    更多塔克创新资讯欢迎登陆[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      WS2812B RGB全彩LED灯珠,只需通过一根信号线控制多个 ...

  4. Root谷歌OnHub路由器

    Exploitee.rs极客小组因破解谷歌电视而成名,并且他们还发布了许多硬件相关的工具和exp.这次他们root了OnHub路由器.并发布在了他们的博客里. Google OnHub是谷歌于今年八月 ...

  5. iOS开发之旅:实现一个APP界面框架

    在上一篇博客中,给大家介绍了一下我们传统的 APP 界面框架-标签导航的一些优缺点,在这篇文章中我会来给大家演示,如何用代码去实现这一框架.本次的实现我会分成俩部分来讲,好了闲话少说,接下来进入到开发 ...

  6. 基础练习 Sine之舞

    基础练习 Sine之舞   时间限制:1.0s   内存限制:512.0MB        问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他 ...

  7. opencv-python教程学习系列10-颜色空间转换

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍颜色空间转换,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统环境 系 ...

  8. CF1096. G. Lucky Tickets(快速幂NTT)

    All bus tickets in Berland have their numbers. A number consists of n digits (n is even). Only k dec ...

  9. JPA 简单实体映射和EntityManagerAPI

    [常用注解] 使用JPA 指定映射关系时,有两种方式,一种是使用xml 方式,另一种是注解方式,笔者推荐使用注解方式.在JPA 映射简单实体时,常用的注解如下: @Entity:修饰实体类对象,表示该 ...

  10. gitlab使用笔记

    一.git环境搭建 1.百度gitlab,找到其官网,注册gitlab账号(需要打开FQ软件),登录. 2.点击Profile Settings,然后点击SSH Keys 3.虚拟机中执行:ssh-k ...