¡¡ÊÖ»ú°æ | ------------È«Õ¾×ÊÔ´ÓÐ,ͳһ½âѹÂë,½âÂëƽ̨£¬ÇëµãÕâÀï ------------ ÉèÊ×Ò³ | ¼ÓÊÕ²Ø
µ±Ç°Î»Ö㺠ÍøÕ¾Ê×Ò³ > jQuery¿â > ÎÄÕ µ±Ç°Î»Ö㺠jQ > ÎÄÕÂ

js´¦Àí»õ±Ò¸ñʽ²å¼þcurrency.js

ʱ¼ä£º2020-06-30    µã»÷£º ´Î    À´Ô´£ºÍøÂç    ×÷ÕߣºØýÃû - С + ´ó

js´¦Àí»õ±Ò¸ñʽ²å¼þcurrency.js
¼òÒª½Ì³Ì

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 the increment 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

 js´¦Àí»õ±Ò¸ñʽ²å¼þcurrency.js ±¾µØÏÂÔØ

ÉÏһƪ£ºBootstrap¹Ì¶¨¶¥²¿µ¼º½²Ëµ¥×Ô¶¯Òþ²Ø²å¼þ

ÏÂһƪ£º´øÊÓ¾õÌØЧµÄjsÃÜÂëÇ¿¶È¼ì²âÌØЧ

ÕãICP±¸18035339ºÅ-15  |   QQ£º79720816  |  µØÖ·£ºÂìÒÏ·ÖÏí£­Ò»¸öÖ»×öÓÐÓõķÖÏí¡£  |  13388629007  |  
Copyright © 2023 ÂìÒÏ·ÖÏíÍø °æȨËùÓУ¬ÊÚȨwww.tanan.netʹÓà Powered by ANTQQ.COM