var decimal_6decFormat = new AW.Formats.Number;
decimal_6decFormat.setTextFormat("###.######");
var doFormat = function(value){
var multiplier = this._multiplier;
var abs = (value<0) ? -value : value;
var delta = (value<0) ? -0.5 : +0.5;
var rounded = (Math.round(value * multiplier) + delta)/multiplier + "";
if (abs<1000) {return rounded.replace(this.p1, this.r1)}
if (abs<1000000) {return rounded.replace(this.p2, this.r2)}
if (abs<1000000000) {return rounded.replace(this.p3, this.r3)}
return rounded.replace(this.p4, this.r4);
};
AW.Formats.Number.create = function(){
var obj = this.prototype;
obj.dataToValue = function(v){
return Number(("" + v).replace(numPattern, ""));
};
obj.textToValue = function(v){
return Number(("" + v).replace(numPattern, ""));
};
var numPattern = /[^0-9.\-+]+/gm;
var noFormat = function(value){
return "" + value;
};
var doFormat = function(value){
var abs = (value<0) ? -value : value;
var rounded = value.toFixed(this._decimals);
if (abs<1000) {return rounded.replace(this.p1, this.r1)}
if (abs<1000000) {return rounded.replace(this.p2, this.r2)}
if (abs<1000000000) {return rounded.replace(this.p3, this.r3)}
return rounded.replace(this.p4, this.r4);
};
obj.setTextFormat = function(format){
var pattern = /^([^0#]*)([0#]*)([ .,]?)([0#]|[0#]{3})([.,])([0#]*)([^0#]*)$/;
var f = format.match(pattern);
if (!f) {
this.valueToText = function(value){return "" + value};
this.dataToText = function(value){return "" + value};
return;
}
this.valueToText = doFormat;
this.dataToText = function(v){return doFormat.call(this, Number(("" + v).replace(numPattern, "")))};
var rs = f[1]; // result start
var rg = f[3]; // result group separator;
var rd = f[5]; // result decimal separator;
var re = f[7]; // result end
this._decimals = f[6].length;
var ps = "^(-?\\d+)", pm = "(\\d{3})", pe = "\\.(\\d{" + this._decimals + "})$";
if (!this._decimals) {
pe = "($)";
rd = "";
}
this.p1 = new RegExp(ps + pe);
this.p2 = new RegExp(ps + pm + pe);
this.p3 = new RegExp(ps + pm + pm + pe);
this.p4 = new RegExp(ps + pm + pm + pm + pe);
this.r1 = rs + "$1" + rd + "$2" + re;
this.r2 = rs + "$1" + rg + "$2" + rd + "$3" + re;
this.r3 = rs + "$1" + rg + "$2" + rg + "$3" + rd + "$4" + re;
this.r4 = rs + "$1" + rg + "$2" + rg + "$3" + rg + "$4" + rd + "$5" + re;
};
obj.setTextFormat("");
};
This topic is archived.