Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6acdad62

Von Moritz Bunkus vor mehr als 9 Jahren hinzugefügt

  • ID 6acdad62e7a4e11c8929e7ab00ce1bac511ed883
  • Vorgänger d5fee534
  • Nachfolger f8ec31c6

kivi.js: format/round/parse_amount, format/parse_date

Unterschiede anzeigen:

js/kivi.js
1 1
namespace("kivi", function(ns) {
2 2
  ns._locale = {};
3
  ns._date_format   = {
4
    sep: '.',
5
    y:   2,
6
    m:   1,
7
    d:   0
8
  };
9
  ns._number_format = {
10
    decimalSep:  ',',
11
    thousandSep: '.'
12
  };
13

  
14
  ns.setup_formats = function(params) {
15
    var res = (params.dates || "").match(/^([ymd]+)([^a-z])([ymd]+)[^a-z]([ymd]+)$/);
16
    if (res) {
17
      ns._date_format                      = { sep: res[2] };
18
      ns._date_format[res[1].substr(0, 1)] = 0;
19
      ns._date_format[res[3].substr(0, 1)] = 1;
20
      ns._date_format[res[4].substr(0, 1)] = 2;
21
    }
22

  
23
    res = (params.numbers || "").match(/^\d*([^\d]?)\d+([^\d])\d+$/);
24
    if (res)
25
      ns._number_format = {
26
        decimalSep:  res[2],
27
        thousandSep: res[1]
28
      };
29
  };
30

  
31
  ns.parse_date = function(date) {
32
    var parts = date.replace(/\s+/g, "").split(ns._date_format.sep);
33
    date     = new Date(
34
      ((parts[ ns._date_format.y ] || 0) * 1) || (new Date).getFullYear(),
35
       (parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based.
36
       (parts[ ns._date_format.d ] || 0) * 1
37
    );
38

  
39
    return isNaN(date.getTime()) ? undefined : date;
40
  };
41

  
42
  ns.format_date = function(date) {
43
    if (isNaN(date.getTime()))
44
      return undefined;
45

  
46
    var parts = [ "", "", "" ]
47
    parts[ ns._date_format.y ] = date.getFullYear();
48
    parts[ ns._date_format.m ] = (date.getMonth() <  9 ? "0" : "") + (date.getMonth() + 1); // Months are 0-based, but days are 1-based.
49
    parts[ ns._date_format.d ] = (date.getDate()  < 10 ? "0" : "") + date.getDate();
50
    return parts.join(ns._date_format.sep);
51
  };
52

  
53
  ns.parse_amount = function(amount) {
54
    if ((amount == undefined) || (amount == ''))
55
      return 0;
56

  
57
    if (ns._number_format.decimalSep == ',')
58
      amount = amount.replace(/\./g, "").replace(/,/g, ".");
59

  
60
    amount = amount.replace(/[\',]/g, "")
61

  
62
    return eval(amount);
63
  };
64

  
65
  ns.round_amount = function(amount, places) {
66
    var neg  = amount >= 0 ? 1 : -1;
67
    var mult = Math.pow(10, places + 1);
68
    var temp = Math.abs(amount) * mult;
69
    var diff = Math.abs(1 - temp + Math.floor(temp));
70
    temp     = Math.floor(temp) + (diff <= 0.00001 ? 1 : 0);
71
    var dec  = temp % 10;
72
    temp    += dec >= 5 ? 10 - dec: dec * -1;
73

  
74
    return neg * temp / mult;
75
  };
76

  
77
  ns.format_amount = function(amount, places) {
78
    amount = amount || 0;
79

  
80
    if ((places != undefined) && (places >= 0))
81
      amount = ns.round_amount(amount, Math.abs(places));
82

  
83
    var parts = ("" + Math.abs(amount)).split(/\./);
84
    var intg  = parts[0];
85
    var dec   = parts.length > 1 ? parts[1] : "";
86
    var sign  = amount  < 0      ? "-"      : "";
87

  
88
    if (places != undefined) {
89
      while (dec.length < Math.abs(places))
90
        dec += "0";
91

  
92
      if ((places > 0) && (dec.length > Math.abs(places)))
93
        dec = d.substr(0, places);
94
    }
95

  
96
    if ((ns._number_format.thousandSep != "") && (intg.length > 3)) {
97
      var len   = ((intg.length + 2) % 3) + 1,
98
          start = len,
99
          res   = intg.substr(0, len);
100
      while (start < intg.length) {
101
        res   += ns._number_format.thousandSep + intg.substr(start, 3);
102
        start += 3;
103
      }
104

  
105
      intg = res;
106
    }
107

  
108
    var sep = (places != 0) && (dec != "") ? ns._number_format.decimalSep : "";
109

  
110
    return sign + intg + sep + dec;
111
  };
3 112

  
4 113
  ns.t8 = function(text, params) {
5 114
    var text = ns._locale[text] || text;

Auch abrufbar als: Unified diff