currency.jsÊÇÒ»¿î js´¦Àí»õ±Ò¸ñʽ¸ñʽ²å¼þ¡£currency.jsÌṩ·Ç³£Áé»îµÄapiÀ´°ïÖúÄú½â¾öjavascriptÖеĸ¡µãÊýÎÊÌ⣬²¢Ìṩ¸ñʽ»¯»õ±ÒÊýÖµ¹¦ÄÜ£¬Ê¹ÓÃÆðÀ´·Ç³£·½±ã¡£
currency.jsʹÓÃEs6Óï·¨±àд£¬Ö»ÓÐÖ§³ÖEs6Óï·¨µÄä¯ÀÀÆ÷²ÅÄÜÕý³£Ê¹Óøòå¼þ¡£
ʹÓ÷½·¨
ÔÚÒ³ÃæÖÐÒýÈëµÄcurrency.jsÎļþ¡£
<script src="js/currency.js"></script>
»ù±¾Ê¹ÓÃ
currency.js¿ÉÒÔ½ÓÊÕÊýÖµ£¬×Ö·û´®»ò»õ±Ò¶ÔÏó×÷Ϊ²ÎÊýÖµ¡£
currency(123); // 123.00currency(1.23); // 1.23currency("1.23") // 1.23currency("$12.30") // 12.30var value = currency("123.45");currency(value); // 123.45
currency.jsÌṩһ×éÊýѧÔËËã·½·¨À´°ïÖúÄú½øÐи¡µãÊý¼ÆËã¡£
currency(123.50).add(0.23); // 123.73currency(5.00).subtract(0.50); // 4.50currency(45.25).multiply(3); // 135.75currency(1.12).distribute(5); // [0.23, 0.23, 0.22, 0.22, 0.22]
currency.jsÄÚÖÃÁ˸ñʽ»¯¹¦ÄÜ£¬ÄÜÕýÈ·µÄÏÔʾ¶ººÅºÍÔ²µã·Ö¸ô·û¡£
currency("2,573,693.75").add("100,275.50").format(); // "2,673,969.25"currency("1,237.72").subtract(300).format(); // "937.72"
ÄãÒ²¿ÉÒÔ×Ô¶¨Òå×Ô¼ºµÄ»õ±ÒÊýÖµ¸ñʽ¡£
var euro = value => currency(value, { separator: ".", decimal: "," });euro("2.573.693,75").add("100.275,50").format(); // "2.673.969,25"euro("1.237,72").subtract(300).format(); // "937,72"
ÅäÖòÎÊý
currency.js¿ÉÓõÄÅäÖòÎÊýÓУº
-
symbol
£º»õ±ÒµÄ·ûºÅ£¬Ä¬ÈÏֵΪ$
¡£currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"
-
symbol
£ºÊÇ·ñÔÚµ÷ÓÃcurrency.format()
·½·¨Ê±Ê¹ÓöººÅ·Ö¸ô·û£¬Ä¬ÈÏΪ,
¡£currency(1234.56, { symbol: ',' }).format(); // => "1,234.56"currency(1234.56, { symbol: ' ' }).format(); // => "1 234.56"
-
decimal
£ºcurrency(1.23, { decimal: '.' }).format(); // => "1.23"currency(1.23, { decimal: ',' }).format(); // => "1,23"
-
precision
£º±£ÁôСÊýµãµÄλÊý¡£Ä¬Èϱ£Áô2λ¡£currency(1.234, { precision: 2 }); // => "1.23"currency(1.234, { precision: 3 }); // => "1.234"
-
formatWithSymbol
£ºÊÇ·ñÔÚµ÷ÓÃcurrency.format()
·½·¨Ê±Ê¹Óûõ±Ò·ûºÅ¡£Ä¬ÈÏֵΪfalse
¡£currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23"currency(1.23, { formatWithSymbol: false }).format(); // => "1.23"
-
errorOnInvalid
£ºµ±´«Èënull
»òundefined
µÈ²»ÕýÈ·µÄֵʱ£¬currency
Å׳öÒì³£¡£Ä¬ÈÏֵΪfalse
¡£currency(undefined, { errorOnInvalid: true }); // throws an error
-
increment
£ºWhen implementing a currency that implements rounding, setting theincrement
value will allow you to set the closest increment to round the display value to.var currencyRounding = value => currency(value, { increment: .05 });currencyRounding(1.09); // => { intValue: 109, value: 1.09 }currencyRounding(1.09).format(); // => "1.10"currencyRounding(1.06); // => { intValue: 106, value: 1.06 }currencyRounding(1.06).format(); // => "1.05"
-
useVedic
£ºÊ¹ÓÃÓ¡¶È±àºÅϵͳÀ´ÏÔʾ»õ±ÒÊýÖµ¡£Ä¬ÈÏÖµÊÇfalse
¡£currency(1234567.89, { useVedic: true }).format(); // => "12,34,567.89"
·½·¨
currency.js¿ÉÓõķ½·¨ÓУº
-
currency.add( value )
£º¼Ó·¨ÔËËã¡£currency(123.45).add(.01); // => "123.46"
-
currency.subtract( value )
£º¼õ·¨ÔËËã¡£currency(123.45).subtract(.01); // => "123.44"
-
currency.multiply( number )
£º³Ë·¨ÔËËã¡£currency(123.45).multiply(2); // => "246.90"
-
currency.divide( number )
£º³ý·¨ÔËËã¡£currency(123.45).divide(2); // => "61.73"
-
currency.distribute( number )
£ºÆ½¾ù·ÖÅä¡£currency(12.35).distribute(3); // => [4.12, 4.12, 4.11]currency(12.00).distribute(3); // => [4.00, 4.00, 4.00]
-
currency.format([ boolean ])
£º·µ»Ø¸ñʽ»¯ºóµÄ»õ±Ò¸ñʽ¡£currency(1000.00).format(); // => "1,000.00"currency("1,234,567/90").add("200,000").format(); // => "1,434,567.89"
ÉÏÃæÊÇĬÈϵĸñʽ£¬Äã¿ÉÒÔͨ¹ýÏÂÃæµÄ·½·¨À´×Ô¶¨Òå»õ±Ò¸ñʽ¡£
var euro = value => currency(value, { separator: ' ', decimal: ',' });// ...euro(1000.00).format(); // => "1 000,00"euro(1234567.89).add("200 000").format(); // => "1 434 567,89"
½áºÏ
symbol
²ÎÊý£¬¿ÉÒÔÏÔʾ³ö´ø·ûºÅµÄ»õ±Ò¸ñʽ¡£var money = value => currency(value, { formatWithSymbol: true });money(1000.00).format(); // => "$1,000.00"money(1000.00).format(false); // => "1,000.00"
-
currency.dollars
£º·µ»Ø»õ±ÒµÄdollarÖµ¡£currency(123.45).dollars(); // => 123currency("0.99").dollars(); // => 0
-
currency.cents
£º·µ»Ø»õ±ÒµÄcentÖµ¡£currency(123.45).cents(); // => 45currency("0.99").cents(); // => 99
- ¹ú¼Ê»¯£¨i18n£©ºÍ¸ñʽ£ºcurrency.jsĬÈÏʹÓõÄÊÇÃÀ¹ú»õ±Ò£¬Äã¿ÉÒÔʹÓÃÏÂÃæµÄ·½·¨À´½«»õ±Ò¸ñʽ±¾µØ»¯¡£
const USD = value => currency(value);const RMB = value => currency(value, { precision: 0, symbol: '£¤' });const EURO = value => currency(value, { symbol: '€', decimal: ',', separator: '.' });USD(1234.567).format(true); // => "$1,234.57"RMB(1234.567).format(true); // => "£¤1,235"EURO(1234.567).format(true); // => "€1.234,57"
currency.js js´¦Àí»õ±Ò¸ñʽ¸ñʽ²å¼þµÄgithubµØַΪ£ºhttps://github.com/scurker/currency.js