currency.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.currency"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.currency"] = true;
  8. dojo.provide("dojo.currency");
  9. dojo.require("dojo.number");
  10. dojo.require("dojo.i18n");
  11. dojo.requireLocalization("dojo.cldr", "currency", null, "ROOT,af,ak,am,ar,asa,az,be,bez,bg,bm,bn,bo,br,brx,bs,ca,cgg,chr,cs,cy,da,dav,de,ebu,ee,el,el-polyton,en,en-au,en-bz,en-ca,en-hk,en-jm,en-mt,en-na,en-nz,en-sg,en-tt,es,es-ec,es-pr,es-us,et,fa,fa-af,ff,fi,fil,fr,fr-ca,ga,gl,gsw,guz,ha,he,hi,hr,hu,is,it,iw,ja,jmc,ka,kab,kam,kde,kea,khq,ki,kln,ko,ksb,lag,lg,lt,luo,luy,lv,mas,mer,mfe,mg,mk,ml,mo,mt,my,naq,nb,nd,ne,nl,nn,no,nyn,om,pa-arab,pa-pk,pl,pt,rm,ro,rof,ru,rwk,saq,seh,ses,sg,sh,shi,shi-tfng,sk,sl,sn,so,sq,sr,sr-latn,sv,sw,te,teo,th,ti,tl,tr,tzm,uk,ur,vi,vun,xog,yo,zh,zh-hans-hk,zh-hant,zh-hant-hk,zh-hk,zh-mo,zh-tw");
  12. dojo.require("dojo.cldr.monetary");
  13. dojo.getObject("currency", true, dojo);
  14. /*=====
  15. dojo.currency = {
  16. // summary: localized formatting and parsing routines for currencies
  17. //
  18. // description: extends dojo.number to provide culturally-appropriate formatting of values
  19. // in various world currencies, including use of a currency symbol. The currencies are specified
  20. // by a three-letter international symbol in all uppercase, and support for the currencies is
  21. // provided by the data in `dojo.cldr`. The scripts generating dojo.cldr specify which
  22. // currency support is included. A fixed number of decimal places is determined based
  23. // on the currency type and is not determined by the 'pattern' argument. The fractional
  24. // portion is optional, by default, and variable length decimals are not supported.
  25. }
  26. =====*/
  27. dojo.currency._mixInDefaults = function(options){
  28. options = options || {};
  29. options.type = "currency";
  30. // Get locale-dependent currency data, like the symbol
  31. var bundle = dojo.i18n.getLocalization("dojo.cldr", "currency", options.locale) || {};
  32. // Mixin locale-independent currency data, like # of places
  33. var iso = options.currency;
  34. var data = dojo.cldr.monetary.getData(iso);
  35. dojo.forEach(["displayName","symbol","group","decimal"], function(prop){
  36. data[prop] = bundle[iso+"_"+prop];
  37. });
  38. data.fractional = [true, false];
  39. // Mixin with provided options
  40. return dojo.mixin(data, options);
  41. };
  42. /*=====
  43. dojo.declare("dojo.currency.__FormatOptions", [dojo.number.__FormatOptions], {
  44. // type: String?
  45. // Should not be set. Value is assumed to be "currency".
  46. // symbol: String?
  47. // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
  48. // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
  49. // currency: String?
  50. // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
  51. // For use with dojo.currency only.
  52. // places: Number?
  53. // number of decimal places to show. Default is defined based on which currency is used.
  54. type: "",
  55. symbol: "",
  56. currency: "",
  57. places: ""
  58. });
  59. =====*/
  60. dojo.currency.format = function(/*Number*/value, /*dojo.currency.__FormatOptions?*/options){
  61. // summary:
  62. // Format a Number as a currency, using locale-specific settings
  63. //
  64. // description:
  65. // Create a string from a Number using a known, localized pattern.
  66. // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements)
  67. // appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr)
  68. // as well as the appropriate symbols and delimiters and number of decimal places.
  69. //
  70. // value:
  71. // the number to be formatted.
  72. return dojo.number.format(value, dojo.currency._mixInDefaults(options));
  73. };
  74. dojo.currency.regexp = function(/*dojo.number.__RegexpOptions?*/options){
  75. //
  76. // summary:
  77. // Builds the regular needed to parse a currency value
  78. //
  79. // description:
  80. // Returns regular expression with positive and negative match, group and decimal separators
  81. // Note: the options.places default, the number of decimal places to accept, is defined by the currency type.
  82. return dojo.number.regexp(dojo.currency._mixInDefaults(options)); // String
  83. };
  84. /*=====
  85. dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
  86. // type: String?
  87. // Should not be set. Value is assumed to be currency.
  88. // currency: String?
  89. // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
  90. // For use with dojo.currency only.
  91. // symbol: String?
  92. // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
  93. // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
  94. // places: Number?
  95. // fixed number of decimal places to accept. The default is determined based on which currency is used.
  96. // fractional: Boolean?|Array?
  97. // Whether to include the fractional portion, where the number of decimal places are implied by the currency
  98. // or explicit 'places' parameter. The value [true,false] makes the fractional portion optional.
  99. // By default for currencies, it the fractional portion is optional.
  100. type: "",
  101. currency: "",
  102. symbol: "",
  103. places: "",
  104. fractional: ""
  105. });
  106. =====*/
  107. dojo.currency.parse = function(/*String*/expression, /*dojo.currency.__ParseOptions?*/options){
  108. //
  109. // summary:
  110. // Convert a properly formatted currency string to a primitive Number,
  111. // using locale-specific settings.
  112. //
  113. // description:
  114. // Create a Number from a string using a known, localized pattern.
  115. // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns)
  116. // are chosen appropriate to the locale, as well as the appropriate symbols and delimiters
  117. // and number of decimal places.
  118. //
  119. // expression: A string representation of a currency value
  120. return dojo.number.parse(expression, dojo.currency._mixInDefaults(options));
  121. };
  122. }