Icontem

File: app/components/test-shopping-cart/filters/currency.js

Recommend this page to a friend!
  Classes of Sergey Beskorovayniy  >  Vuex Examples  >  app/components/test-shopping-cart/filters/currency.js  >  Download  
File: app/components/test-shopping-cart/filters/currency.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Vuex Examples
Example apps using Vuex state management pattern
Author: By
Last change:
Date: 2 years ago
Size: 998 bytes
 

Contents

Class file image Download
define([], function () {

    var currency = function (value, currency, decimals) {
        
        var digitsRE = /(\d{3})(?=\d)/g;
        
        value = parseFloat(value);
        if (!isFinite(value) || (!value && value !== 0)){
            return '';
        }
            
        currency = currency ? currency : '$';
        decimals = decimals ? decimals : 2;
        var stringified = Math.abs(value).toFixed(decimals);
        var _int = decimals
                ? stringified.slice(0, -1 - decimals)
                : stringified;
        var i = _int.length % 3;
        var head = i > 0
                ? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
                : '';
        var _float = decimals
                ? stringified.slice(-1 - decimals)
                : '';
        var sign = value < 0 ? '-' : '';
        return sign + currency + head +
                _int.slice(i).replace(digitsRE, '$1,') +
                _float;
    };
    
    return currency;

});