JavaScript-Tool:Numeral.js

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();
// 1000

var 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();
// 2000

var bVal = b.value();
// 1000

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="new_file.vue"></script> </head> <body> <div id="app"> <button @click="reset">还原</button> <div v-noData="{goodsList}"> <ul> <li><span>商品名称</span><span>价格</span><span>操作</span></li> <li v-for="(good,index) in goodsList" :key="index"> <span>{{good.name}}</span> <span>{{good.price|filterPrice }}</span> <span @click="delood(index)" style="color: red;cursor: pointer;">删除</span> </li> </ul> </div> </div> <script> const goodsMixin = { data(){ return{ goodsList:[ {name:'电饭煲',price:200.133232}, {name:'电视机',price:880.998392}, {name:'电冰箱',price:650.2034}, {name:'电脑',price:4032.9930}, {name:'电磁炉',price:210.4322} ] } }, methods: { delGood(index){ this.goodsList.splice(index,1) }, reset(){ this.goodsList = [ {name:'电饭煲',price:200.133232}, {name:'电视机',price:880.998392}, {name:'电冰箱',price:650.2034}, {name:'电脑',price:4032.9930}, {name:'电磁炉',price:210.4322} ] } }, filters:{ filterPrice(value){ let newValue = [] newValue = value.toLocaleString().split('.') newValue[0] = (newValue[0] + '').replace(/\d{1,3}(?=(\d{3})+$)/g,'$&') return newValue[0] + '.' + newValue[1]/slice(0,2); } } } new Vue({ el:'app', mixins:[goodsMixin], directives:{ nodata:{ inserted(el,binding){ const noData = document.createElement('div'); noData.classList = 'noData' noData.innerHTML = '暂无数据'; el.appendChild(noData) const{goodsList} = binding.value; noData.style.display = goodsList.length === 0 ?'blick' : 'none' }, updata(el,binding){ const{goodsList} = binding.value; el.querySelector('.noData').style.display = goodsList.length === 0 ?'blick' : 'none'; } } }, data(){ return{} } }) </script> <style> #app ul li{ list-style: none; width: 400px; display: flex; justify-content: space-between; } .noData{ width: 400px; text-align: center; margin-left: 40px; } </style> </body> </html>纠错
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值