currency.js 5.0 KB

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