JavaScript - Zahlen formatieren

Aus Wikizone
Version vom 26. Juli 2013, 16:01 Uhr von 134.3.241.116 (Diskussion) (Die Seite wurde neu angelegt: „Hier gibt es schicke Tools und Plugins. Etwa für jQuery https://code.google.com/p/jquery-numberformatter/ Aber oft reicht auch eine kleine Funktion z.B. diese…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Hier gibt es schicke Tools und Plugins. Etwa für jQuery

https://code.google.com/p/jquery-numberformatter/

Aber oft reicht auch eine kleine Funktion z.B. diese:

Quelle: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript?page=1&tab=votes#tab-top (2013-07)

/* 
decimal_sep: character used as deciaml separtor, it defaults to '.' when omitted
thousands_sep: char used as thousands separator, it defaults to ',' when omitted
*/
Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep)
{ 
   var n = this,
   c = isNaN(decimals) ? 2 : Math.abs(decimals), //if decimal is zero we must take it, it means user does not want to show any decimal
   d = decimal_sep || '.', //if no decimal separator is passed we use the dot as default decimal separator (we MUST use a decimal separator)

   /*
   according to [http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function]
   the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
   rather than doing value === undefined.
   */   
   t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, //if you don't want to use a thousands separator you can pass empty string as thousands_sep value

   sign = (n < 0) ? '-' : '',

   //extracting the absolute value of the integer part of the number and converting to string
   i = parseInt(n = Math.abs(n).toFixed(c)) + '', 

   j = ((j = i.length) > 3) ? j % 3 : 0; 
   return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); 
}

Anwendung:

price = parseFloat(price).toMoney(2, ',', '.');

In diesem Fall war price vorher ein String und wird deshalb über parseFloat zuerst in eine Zahl umgewandelt.

Parameter:

Anzahl der Ziffern nach dem Komma
Komma - Trennzeichen
Tausender - Trennzeichen