currency.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define("dojo/currency", ["./_base/kernel", "./_base/lang", "./_base/array", "./number", "./i18n", "./i18n!./cldr/nls/currency", "./cldr/monetary"], function(dojo, lang, darray, dnumber, i18n, nlsCurrency, cldrMonetary) {
  2. // module:
  3. // dojo/currency
  4. // summary:
  5. // TODOC
  6. lang.getObject("currency", true, dojo);
  7. /*=====
  8. dojo.currency = {
  9. // summary: localized formatting and parsing routines for currencies
  10. //
  11. // description: extends dojo.number to provide culturally-appropriate formatting of values
  12. // in various world currencies, including use of a currency symbol. The currencies are specified
  13. // by a three-letter international symbol in all uppercase, and support for the currencies is
  14. // provided by the data in `dojo.cldr`. The scripts generating dojo.cldr specify which
  15. // currency support is included. A fixed number of decimal places is determined based
  16. // on the currency type and is not determined by the 'pattern' argument. The fractional
  17. // portion is optional, by default, and variable length decimals are not supported.
  18. }
  19. =====*/
  20. dojo.currency._mixInDefaults = function(options){
  21. options = options || {};
  22. options.type = "currency";
  23. // Get locale-dependent currency data, like the symbol
  24. var bundle = i18n.getLocalization("dojo.cldr", "currency", options.locale) || {};
  25. // Mixin locale-independent currency data, like # of places
  26. var iso = options.currency;
  27. var data = cldrMonetary.getData(iso);
  28. darray.forEach(["displayName","symbol","group","decimal"], function(prop){
  29. data[prop] = bundle[iso+"_"+prop];
  30. });
  31. data.fractional = [true, false];
  32. // Mixin with provided options
  33. return lang.mixin(data, options);
  34. };
  35. /*=====
  36. dojo.declare("dojo.currency.__FormatOptions", [dojo.number.__FormatOptions], {
  37. // type: String?
  38. // Should not be set. Value is assumed to be "currency".
  39. // symbol: String?
  40. // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
  41. // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
  42. // currency: String?
  43. // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
  44. // For use with dojo.currency only.
  45. // places: Number?
  46. // number of decimal places to show. Default is defined based on which currency is used.
  47. type: "",
  48. symbol: "",
  49. currency: "",
  50. places: ""
  51. });
  52. =====*/
  53. dojo.currency.format = function(/*Number*/value, /*dojo.currency.__FormatOptions?*/options){
  54. // summary:
  55. // Format a Number as a currency, using locale-specific settings
  56. //
  57. // description:
  58. // Create a string from a Number using a known, localized pattern.
  59. // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements)
  60. // appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr)
  61. // as well as the appropriate symbols and delimiters and number of decimal places.
  62. //
  63. // value:
  64. // the number to be formatted.
  65. return dnumber.format(value, dojo.currency._mixInDefaults(options));
  66. };
  67. dojo.currency.regexp = function(/*dojo.number.__RegexpOptions?*/options){
  68. //
  69. // summary:
  70. // Builds the regular needed to parse a currency value
  71. //
  72. // description:
  73. // Returns regular expression with positive and negative match, group and decimal separators
  74. // Note: the options.places default, the number of decimal places to accept, is defined by the currency type.
  75. return dnumber.regexp(dojo.currency._mixInDefaults(options)); // String
  76. };
  77. /*=====
  78. dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
  79. // type: String?
  80. // Should not be set. Value is assumed to be currency.
  81. // currency: String?
  82. // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
  83. // For use with dojo.currency only.
  84. // symbol: String?
  85. // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
  86. // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
  87. // places: Number?
  88. // fixed number of decimal places to accept. The default is determined based on which currency is used.
  89. // fractional: Boolean?|Array?
  90. // Whether to include the fractional portion, where the number of decimal places are implied by the currency
  91. // or explicit 'places' parameter. The value [true,false] makes the fractional portion optional.
  92. // By default for currencies, it the fractional portion is optional.
  93. type: "",
  94. currency: "",
  95. symbol: "",
  96. places: "",
  97. fractional: ""
  98. });
  99. =====*/
  100. dojo.currency.parse = function(/*String*/expression, /*dojo.currency.__ParseOptions?*/options){
  101. //
  102. // summary:
  103. // Convert a properly formatted currency string to a primitive Number,
  104. // using locale-specific settings.
  105. //
  106. // description:
  107. // Create a Number from a string using a known, localized pattern.
  108. // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns)
  109. // are chosen appropriate to the locale, as well as the appropriate symbols and delimiters
  110. // and number of decimal places.
  111. //
  112. // expression: A string representation of a currency value
  113. return dnumber.parse(expression, dojo.currency._mixInDefaults(options));
  114. };
  115. return dojo.currency;
  116. });