/tags/release/7020-RC2/design/htmlmock/javascript/dojo/currency.js

http://aipo.googlecode.com/ · JavaScript · 103 lines · 29 code · 12 blank · 62 comment · 3 complexity · a075450a6d3edd145e724946a3977340 MD5 · raw file

  1. if(!dojo._hasResource["dojo.currency"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  2. dojo._hasResource["dojo.currency"] = true;
  3. dojo.provide("dojo.currency");
  4. dojo.require("dojo.number");
  5. dojo.require("dojo.i18n");
  6. dojo.requireLocalization("dojo.cldr", "currency", null, "ROOT,de,en,en-au,en-ca,en-us,es,fr,it,ja,ko,pt,zh");
  7. dojo.require("dojo.cldr.monetary");
  8. dojo.currency._mixInDefaults = function(options){
  9. options = options || {};
  10. options.type = "currency";
  11. // Get locale-depenent currency data, like the symbol
  12. var bundle = dojo.i18n.getLocalization("dojo.cldr", "currency", options.locale) || {};
  13. // Mixin locale-independent currency data, like # of places
  14. var iso = options.currency;
  15. var data = dojo.cldr.monetary.getData(iso);
  16. dojo.forEach(["displayName","symbol","group","decimal"], function(prop){
  17. data[prop] = bundle[iso+"_"+prop];
  18. });
  19. data.fractional = [true, false];
  20. // Mixin with provided options
  21. return dojo.mixin(data, options);
  22. }
  23. dojo.currency.format = function(/*Number*/value, /*Object?*/options){
  24. // summary:
  25. // Format a Number as a String, using locale-specific settings
  26. //
  27. // description:
  28. // Create a string from a Number using a known localized pattern.
  29. // Formatting patterns appropriate to the locale are chosen from the CLDR http://unicode.org/cldr
  30. // as well as the appropriate symbols and delimiters. See http://www.unicode.org/reports/tr35/#Number_Elements
  31. //
  32. // value:
  33. // the number to be formatted.
  34. //
  35. // options: object {currency: String, pattern: String?, places: Number?, round: Number?, symbol: String?, locale: String?}
  36. // currency- the ISO4217 currency code, a three letter sequence like "USD"
  37. // See http://en.wikipedia.org/wiki/ISO_4217
  38. // symbol- override currency symbol. Normally, will be looked up in table of supported currencies, and ISO currency code will
  39. // be used if not found. See dojo.i18n.cldr.nls->currency.js
  40. // pattern- override formatting pattern with this string (see dojo.number.applyPattern)
  41. // places- fixed number of decimal places to show. Default is defined by the currency.
  42. // round- 5 rounds to nearest .5; 0 rounds to nearest whole (default). -1 means don't round.
  43. // locale- override the locale used to determine formatting rules
  44. return dojo.number.format(value, dojo.currency._mixInDefaults(options));
  45. }
  46. dojo.currency.regexp = function(/*Object?*/options){
  47. //
  48. // summary:
  49. // Builds the regular needed to parse a number
  50. //
  51. // description:
  52. // Returns regular expression with positive and negative match, group and decimal separators
  53. //
  54. // options: object {pattern: String, locale: String, strict: Boolean, places: mixed}
  55. // currency- the ISO4217 currency code, a three letter sequence like "USD"
  56. // See http://en.wikipedia.org/wiki/ISO_4217
  57. // symbol- override currency symbol. Normally, will be looked up in table of supported currencies, and ISO currency code will
  58. // be used if not found. See dojo.i18n.cldr.nls->currency.js
  59. // pattern- override pattern with this string
  60. // locale- override the locale used to determine formatting rules
  61. // strict- strict parsing, false by default
  62. // places- number of decimal places to accept. Default is defined by currency.
  63. return dojo.number.regexp(dojo.currency._mixInDefaults(options)); // String
  64. }
  65. dojo.currency.parse = function(/*String*/expression, /*Object?*/options){
  66. //
  67. // summary:
  68. // Convert a properly formatted string to a primitive Number,
  69. // using locale-specific settings.
  70. //
  71. // description:
  72. // Create a Number from a string using a known localized pattern.
  73. // Formatting patterns are chosen appropriate to the locale.
  74. // Formatting patterns are implemented using the syntax described at *URL*
  75. //
  76. // expression: A string representation of a Number
  77. //
  78. // options: object {pattern: string, locale: string, strict: boolean}
  79. // currency- the ISO4217 currency code, a three letter sequence like "USD"
  80. // See http://en.wikipedia.org/wiki/ISO_4217
  81. // symbol- override currency symbol. Normally, will be looked up in table of supported currencies, and ISO currency code will
  82. // be used if not found. See dojo.i18n.cldr.nls->currency.js
  83. // pattern- override pattern with this string
  84. // locale- override the locale used to determine formatting rules
  85. // strict- strict parsing, false by default
  86. // places- number of decimal places to accept. Default is defined by currency.
  87. // fractional- where places are implied by pattern or explicit 'places' parameter, whether to include the fractional portion.
  88. // By default for currencies, it the fractional portion is optional.
  89. return dojo.number.parse(expression, dojo.currency._mixInDefaults(options));
  90. }
  91. }