![]() |
|
Demo af følgende javascript funktionalitetDe følgende demo samples viser dig hvordan man opretter en singleton i javascript og hvordan man kan kode en dato formatter og formatere et beløb med det locale der er gældende for dit område
KildekodenCopyrigt notice: Du må gerne kopiere denne kildekode, så længe du beholder copyright notice intakt Den fulde kildekode for: AmountFormatThe full source code for: AmountFormat.js.php.html1:<? 2:/** 3: * This class simulates a AmountFormat object, in a very primitive matter 4: * 5: * class AmountFormat { 6: * public static AmountFormat getInstance(); // Use the current Locale 7: * public static AmountFormat getInstance(Locale locale); 8: * public static String format(String number); 9: * } 10: * 11: * @package javascript 12: * @author http://Finn-Rasmussen.com 13: * @copyright http://Finn-Rasmussen.com 14: * @version 1.10 15: * @since 22-feb-2007 16: */ 17: 18:/** 19: * The constructor function simulates a crude AmountFormat object 20: * The AmountFormat object is instanciated with the singleton pattern 21: * The Singleton pattern is simulated in JavaScript by: 22: * - Using an anonymous constructor to create a function object 23: * - And saving the returned object in an external variable 24: * <code> 25: * usage: 26: * // Include and instanciate the AmountFormat class as a singleton 27: * <script type="text/javascript" src="../page/AmountFormat.js.php"></script> 28: * 29: * // Use the AmountFormat singleton class with the default Locale 30: * var value = AmountFormat.format('1234567.89'); // value=1,234,567.89 31: * Or 32: * var locale = Locale.getInstance(LanguageCode.DANISH, CountryCode.DK); 33: * var amountformat = AmountFormat.getInstance(locale); 34: * var formatted = amountformat.format('1234567,89'); 35: * </code> 36: */ 37:?> 38:var AmountFormat = new function() { 39: // Consts, do NOT change 40: this.PERIOD = '.'; 41: this.COMMA = ','; 42: 43: // local vars to customize to your locale: I.e. 1,234,567.89 44: this.locale = Locale.getInstance(); 45:<? 46: /** 47: * Get the instance of the current AmountFormat object 48: * @param Locale locale The Locale object 49: * @return AmountFormat The current default AmountFormat object 50: */ 51:?> 52: this.getInstance = function(locale) { 53: if (typeof locale !== 'undefined') { 54: this.locale = locale; 55: } else { 56: alert(typeof locale); 57: } 58: return this; 59: } 60:<? 61: /** 62: * Format the amount with decimal point and thousand seperator 63: * This is the entry methode you must use 64: * usage: 65: * var value = AmountFormat.format('1234567.89'); // value=1,234,567.89 66: * </code> 67: * @public 68: * @param String amount The number to format 69: * @return String The number formatted like 1.234.567,89 70: */ 71:?> 72: this.format = function(number) { 73: var n = this.parse(number); 74: var i = parseFloat(n); 75: var s = this.parseString(i); 76: var sign = i<0?'-':''; 77: return sign + this.addThousandSeperators(s); 78: } 79:<? 80: // THE following methods are private helpers 81: 82: /** 83: * Parse the amount into a string of digits and decimals seperated by a period 84: * All the thousand seperators and the comma decimal point seperators are removed 85: * @private 86: * @param String number The number to parse 87: * @return String The number parsed into: digits+'.'+decimals 88: */ 89:?> 90: this.parse = function(number) { 91: var decimals = ''; 92: var prefix = ''; 93: var d = number.split(this.locale.getDecimalPoint()); 94: switch (d.length) { 95: case 2: 96: decimals = d[1]; 97: case 1: 98: prefix = d[0]; 99: break; 100: default: 101: alert('Wrong DECIMAL_POINT, Expected=digits'+this.locale.getDecimalPoint()+'decimals found='+number); 102: break; 103: } 104: // Strip off any thousand seperators and return a real javascript number like: 1234567.89 105: var digits = this.stripOfThousandSeperators(prefix); 106: s = digits + this.PERIOD + decimals; 107: return s; 108: } 109:<? 110: /** 111: * Parse the amount into a string of digits and decimals seperated by a period 112: * All the thousand seperators and the comma decimal point seperators are removed 113: * @private 114: * @param String number The number to parse 115: * @return String The number parsed into: digits+'.'+decimals 116: */ 117:?> 118: this.parseString = function(number) { 119: var n = number; 120: if (isNaN(n) ) { 121: alert('Wrong format, Expected=digits'+this.locale.getDecimalPoint()+'decimals, found='+number); 122: n = 0.00; 123: } 124: i = Math.abs(n); 125: i = parseInt((i + .005) * 100, 10); 126: i = i / 100; 127: s = new String(i); 128: if (s.indexOf(this.PERIOD) < 0) { 129: s += this.PERIOD+'00'; 130: } 131: if (s.indexOf(this.PERIOD) == (s.length - 2)) { 132: s += '0'; 133: } 134: return s; 135: } 136:<? 137: /** 138: * Strip off any thousand seperators 139: * @private 140: * @param String number The number to strip of the thousand seperators 141: * @return String The number with out any thousand seperators 142: */ 143:?> 144: this.stripOfThousandSeperators = function(number) { 145: var digits = ''; 146: var t = number.split(this.locale.getThousandSeperator()); 147: for (var i=0; i < t.length; i++) { 148: digits += t[i]; 149: } 150: return digits; 151: } 152:<? 153: /** 154: * Update the decimal point, Add the thousand seperators, if defined 155: * @private 156: * @param String number The number to update 157: * @return String The updated number 158: */ 159:?> 160: this.addThousandSeperators = function(number) { 161: var s = number.replace(RegExp('[\\'+this.PERIOD+']',"g"),this.locale.getDecimalPoint()); 162: var decimals = s.split(this.locale.getDecimalPoint()); 163: var t = ''; 164: if (this.locale.getThousandSeperator() !== '') { 165: // Add thousand seperators 1.234.567,89 166: var size = decimals[0].length; 167: for (var i=0; i < size; i++) { 168: t = decimals[0].substr((size-1)-i, 1) + t; 169: if ((i+1) % 3) { 170: // Ignore 171: } else { 172: if (i < size - 1) { 173: t = this.locale.getThousandSeperator() + t; 174: } 175: } 176: } 177: } else { 178: t = s; 179: } 180: if (decimals.length === 2) { 181: t += this.locale.getDecimalPoint() + decimals[1] 182: } 183: return t; 184: } 185: 186:} CountryCode Den fulde kildekode for: CountryCode.js.php.html1:<? 2:/** 3: * This class simulates a CountryCode object, in a very primitive matter 4: * 5: * class CountryCode { 6: * public static final String DK = "DK"; 7: * public static final String NO = "NO"; // Etc. 8: * } 9: * 10: * @package javascript 11: * @author http://Finn-Rasmussen.com 12: * @copyright http://Finn-Rasmussen.com 13: * @version 1.10 14: * @since 22-feb-2007 15: */ 16: 17:/** 18: * The supported Country Codes 19: * @see http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html 20: * @see http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html 21: * The constructor function simulates a crude |