ho deciso di riscrivere completamente esempio ho fatto nel 2009. Si prega di verificare diff se interessati a più vecchia versione. Per ottenere funzionalità come la risposta precedente, ho estratto una parte della libreria Money
su cui sto lavorando.
Inoltre non ricordo perché ho ricreato l'ultima volta toFixed
come quel metodo era già presente. Questa volta non è incluso.
Invece di fare scherzi con String
e Number
oggetti in JavaScript, come l'ultima volta, io creo nuovi, Money
, oggetto.
(function() {
window.Money = (function() {
Money.prototype.amount = 0.0;
Money.prototype.fraction_count = 2;
Money.prototype.fraction_separator = ",";
Money.prototype.separate_thousands = true;
Money.prototype.symbol = "€";
Money.prototype.symbol_position = "front";
Money.prototype.symbol_spacing = false;
Money.prototype.thousands_separator = ".";
function Money(amount, options) {
var o;
if (options == null) {
options = {};
}
for (o in options) {
this[o] = options[o];
}
amount = parseFloat(amount);
if (!isNaN(amount)) {
this.amount = amount;
}
this.format();
}
Money.prototype.format = function() {
this.string_amount = this.amount.toFixed(this.fraction_count);
if (this.separate_thousands) {
this.string_amount = this.separateThousands();
}
return this.string = this.addSymbol();
};
Money.prototype.separateThousands = function() {
var after_dot, before_dot, pattern, _ref;
_ref = this.string_amount.split("."), before_dot = _ref[0], after_dot = _ref[1];
pattern = /(-?\d+)(\d{3})/;
while (pattern.test(before_dot)) {
before_dot = before_dot.replace(pattern, "$1" + this.thousands_separator + "$2");
}
return [before_dot, after_dot].join(this.fraction_separator);
};
Money.prototype.addSymbol = function() {
var string;
string = [this.string_amount];
string.splice((this.symbol_position === "front" ? 0 : 1), 0, this.symbol);
return string.join(this.symbol_spacing ? " " : "");
};
return Money;
})();
Ora, ho bisogno di modificare e/o Number
String
oggetti con dimensioni leggermente e aggiungere toMoney
metodo.
Number.prototype.toMoney = function(options) {
return new Money(this, options);
};
String.prototype.toMoney = function(options) {
return new Money(this, options);
};
Così, finalmente, possiamo convertire String
e/o Number
-Money
e scriverlo fuori come String
di nuovo.
x = "1234567890.0987654321".toMoney();
y = 1234567890.0987654321.toMoney({fraction_count: 5, symbol: "$", symbol_position: "back"});
console.log(x);
// Money {amount: 1234567890.0987654, string_amount: "1.234.567.890,10", string: "€1.234.567.890,10"}
console.log(x.string)
// €1.234.567.890,10
console.log(y);
// Money {fraction_count: 5, symbol: "$", symbol_position: "back", amount: 1234567890.0987654, string_amount: "1.234.567.890,09877"…}
console.log(y.string)
// 1.234.567.890,09877$
Penso che questa soluzione sia molto meglio dell'ultima che ho scritto. Per esempio di lavoro controllare jsFiddle.
FWIW c'è ora un costruito in funzione in JavaScript che fa questo: https://developer.mozilla.org/en-US/docs/Web/ JavaScript/Riferimento/Global_Objects/NumberFormat, che funziona anche nel nodo - come da 0.12 – Simon