You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

number_format.js 4.9 KiB

12 vuotta sitten
11 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. if(!window.wn) wn = {};
  4. function flt(v, decimals, number_format) {
  5. if(v==null || v=='')return 0;
  6. if(typeof v!=="number") {
  7. v = v + "";
  8. // strip currency symbol if exists
  9. if(v.indexOf(" ")!=-1) {
  10. v = v.split(" ")[1];
  11. }
  12. v = strip_number_groups(v, number_format);
  13. v=parseFloat(v);
  14. if(isNaN(v))
  15. v=0;
  16. }
  17. if(decimals!=null)
  18. return _round(v, decimals);
  19. return v;
  20. }
  21. function cint(v, def) {
  22. if(v===true)
  23. return 1;
  24. if(v===false)
  25. return 0;
  26. v=v+'';
  27. if(v!=="0")v=lstrip(v, ['0']);
  28. v=parseInt(v);
  29. if(isNaN(v))v=def===undefined?0:def;
  30. return v;
  31. }
  32. function strip_number_groups(v, number_format) {
  33. if(!number_format) number_format = get_number_format();
  34. // strip groups (,)
  35. if(get_number_format_info(number_format).group_sep==".") {
  36. v = v.replace(/\./g,'');
  37. // sanitize decimal separator to .
  38. v = v.replace(/,/g, ".");
  39. } else {
  40. v=v.replace(/,/g,'');
  41. }
  42. return v;
  43. }
  44. wn.number_format_info = {
  45. "#,###.##": {decimal_str:".", group_sep:",", precision:2},
  46. "#.###,##": {decimal_str:",", group_sep:".", precision:2},
  47. "# ###.##": {decimal_str:".", group_sep:" ", precision:2},
  48. "#,###.###": {decimal_str:".", group_sep:",", precision:3},
  49. "#,##,###.##": {decimal_str:".", group_sep:",", precision:2},
  50. "#.###": {decimal_str:"", group_sep:".", precision:0},
  51. "#,###": {decimal_str:"", group_sep:",", precision:0},
  52. }
  53. window.format_number = function(v, format, decimals){
  54. if (!format) {
  55. format = get_number_format();
  56. if(decimals == null) decimals = cint(wn.defaults.get_default("float_precision")) || 3;
  57. }
  58. info = get_number_format_info(format);
  59. //Fix the decimal first, toFixed will auto fill trailing zero.
  60. decimals = decimals || info.precision;
  61. v = flt(v, decimals, format);
  62. if(v<0) var is_negative = true;
  63. v = Math.abs(v);
  64. v = v.toFixed(decimals);
  65. var part = v.split('.');
  66. // get group position and parts
  67. var group_position = info.group_sep ? 3 : 0;
  68. if (group_position) {
  69. var integer = part[0];
  70. var str = '';
  71. var offset = integer.length % group_position;
  72. for (var i=integer.length; i>=0; i--) {
  73. var l = replace_all(str, info.group_sep, "").length;
  74. if(format=="#,##,###.##" && str.indexOf(",")!=-1) { // INR
  75. group_position = 2;
  76. l += 1;
  77. }
  78. str += integer.charAt(i);
  79. if (l && !((l+1) % group_position) && i!=0 ) {
  80. str += info.group_sep;
  81. }
  82. }
  83. part[0] = str.split("").reverse().join("");
  84. }
  85. if(part[0]+""=="") {
  86. part[0]="0";
  87. }
  88. // join decimal
  89. part[1] = part[1] ? (info.decimal_str + part[1]) : "";
  90. // join
  91. return (is_negative ? "-" : "") + part[0] + part[1];
  92. };
  93. function format_currency(v, currency, decimals) {
  94. var format = get_number_format(currency);
  95. var symbol = get_currency_symbol(currency);
  96. if(symbol)
  97. return symbol + " " + format_number(v, format, decimals);
  98. else
  99. return format_number(v, format, decimals);
  100. }
  101. function get_currency_symbol(currency) {
  102. if(wn.boot) {
  103. if(wn.boot.sysdefaults.hide_currency_symbol=="Yes")
  104. return null;
  105. if(!currency)
  106. currency = wn.boot.sysdefaults.currency;
  107. return wn.model.get_value(":Currency", currency, "symbol") || currency;
  108. } else {
  109. // load in template
  110. return wn.currency_symbols[currency];
  111. }
  112. }
  113. var global_number_format = null;
  114. function get_number_format(currency) {
  115. if(!global_number_format) {
  116. global_number_format = wn.boot.sysdefaults.number_format
  117. || wn.model.get_value("Currency", wn.boot.sysdefaults.currency, "number_format")
  118. || "#,###.##";
  119. }
  120. var number_format;
  121. if(currency && wn.boot) {
  122. number_format = wn.model.get_value("Currency", currency,
  123. "number_format");
  124. }
  125. return number_format || global_number_format;
  126. }
  127. function get_number_format_info(format) {
  128. var info = wn.number_format_info[format];
  129. if(!info) {
  130. info = {decimal_str:".", group_sep:",", precision:2};
  131. }
  132. return info;
  133. }
  134. function _round(num, precision) {
  135. var d = cint(precision);
  136. var m = Math.pow(10, d);
  137. var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
  138. var i = Math.floor(n), f = n - i;
  139. var r = (f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n);
  140. return d ? r / m : r;
  141. }
  142. function roundNumber(num, precision) {
  143. // backward compatibility
  144. return _round(num, precision);
  145. }
  146. function precision(fieldname, doc) {
  147. if(!doc) doc = cur_frm.doc;
  148. var df = wn.meta.get_docfield(doc.doctype, fieldname, doc.parent || doc.name);
  149. if(!df) console.log(fieldname + ": could not find docfield in method precision()");
  150. return wn.meta.get_field_precision(df, doc);
  151. }
  152. var lstrip = function(s, chars) {
  153. if(!chars) chars = ['\n', '\t', ' '];
  154. // strip left
  155. var first_char = s.substr(0,1);
  156. while(in_list(chars, first_char)) {
  157. var s = s.substr(1);
  158. first_char = s.substr(0,1);
  159. }
  160. return s;
  161. }
  162. function in_list(list, item) {
  163. if(!list) return false;
  164. for(var i=0, j=list.length; i<j; i++)
  165. if(list[i]==item) return true;
  166. return false;
  167. }