Suggestion: improve grid method setNumberFormat

I’ve heard that this forum is not only for bugs discussions, but suggestions are also welcome.
If so I would like to provide small improvement for the grid method mentioned above.

What I believe is that this method should have a bit smarter logic for cases when number format mask defined slightly incorrect. This would do grid functionality more stable and sensible in case mask provided on the fly withing script execution.

[code]setNumberFormat:function(mask, cInd, p_sep, d_sep){
var nmask = mask.replace(/[^0,.]*/g, “”);
var pfix = nmask.indexOf(".");

	if (pfix > -1)
		pfix=nmask.length-pfix-1;
	var dfix = nmask.indexOf(",");

	if (dfix > -1)
		dfix=nmask.length-pfix-2-dfix;
	if (typeof p_sep != "string")
		p_sep=this.i18n.decimal_separator;
	if (typeof d_sep != "string")
		d_sep=this.i18n.group_separator;
	
	//var pref = mask.split(nmask)[0];
	//var postf = mask.split(nmask)[1];
	
	//THE ABOVE COMMENTED TO BE REPLACED WITH THE FOLLOWING NEW CODE:
	
	//!!!START OF NEW CODE!!!//
	var prefInd = mask.indexOf(nmask[0]);
	var pref = mask.substring(0,prefInd);
	var cmask = mask;
	for (var i=0;i<nmask.length;i++){	
		var chi = cmask.indexOf(nmask[i]);
        if (chi<cmask.length-1){
			cmask = cmask.substring(chi+1);
		}else{
			cmask = "";
		}	
	}
	var postf = cmask;
	//!!!END OF NEW CODE!!!//
	
	this._maskArr[cInd]=[
		pfix,
		dfix,
		pref,
		postf,
		p_sep,
		d_sep
	];
}

[/code]

Example:
Incorrect mask expression defined to test method: mask = “A0B,C000D.E00F”
Desired format: nmask = “0,000.00”
Number to be formatted: num = 1125.125
Original version result: orgResult = A0B,C000D.E00F1,125.12
Modified version result: modResult = A1,125.12F

I could provide explanation if required.

May be a better implementation of suggested logic exists, but to my opinion the logic itself has more sense comparing to original behavior.
Thank you in advance for your consideration.

Few more amendments to method setNumberFormat: dhtmlXGridObject.prototype.setNumberFormat=function(mask, cInd, p_sep, d_sep){ var nmask = mask.replace(/[^0\,\.]*/g, ""); var pfixInd = nmask.indexOf("."); var pfix=0; if (pfixInd > -1){ pfix=nmask.length-pfixInd-1; } var dfixInd = nmask.indexOf(","); var dfix=0; if (dfixInd > -1 && pfixInd>dfixInd){ dfix=nmask.length-pfix-2-dfixInd; } if (typeof p_sep != "string") p_sep=this.i18n.decimal_separator; if (typeof d_sep != "string") d_sep=this.i18n.group_separator; var prefInd = mask.indexOf(nmask[0]); var pref = mask.substring(0,prefInd); var cmask = mask; for (var i=0;i<nmask.length;i++){ var chi = cmask.indexOf(nmask[i]); if (chi<cmask.length-1){ cmask = cmask.substring(chi+1); }else{ cmask = ""; break; } } var postf = cmask; this._maskArr[cInd]=[ pfix, dfix, pref, postf, p_sep, d_sep ]; }

in conjunction with associated method _aplNF revised:

dhtmlXGridObject.prototype._aplNF=function(data, ind){ var a = this._maskArr[ind]; if (!a) return data; var c = (parseFloat(data) < 0 ? "-" : "")+a[2]; data=Math.abs(Math.round(parseFloat(data)*Math.pow(10, a[0] > 0 ? a[0] : 0))).toString(); data=((data.length< a[0])? Math.pow(10, a[0]+1-data.length).toString().substr(1, a[0]+1)+data.toString(): data).split("").reverse(); //data[a[0]]=(data[a[0]]||"0")+a[4]; //--CORRECTION--THE ABOVE LINE IS REPLACED WITH THE FOLLOWING LINE: data[a[0]]=(data[a[0]]||"0")+((a[0]>0)?a[4]:""); if (a[1] > 0){ for (var j = (a[0] > 0 ? 0 : 1)+a[0]+a[1]; j < data.length; j+=a[1])data[j]+=a[5]; } return c+data.reverse().join("")+a[3]; }

Probably method _aplNFb needs corrections as well, unfortunately I haven’t look at it yet. At a glance I found out that it did not take into account possible prefix and suffix added in _aplNF - once we add anything in apply number format method, then we have to remove it in apply back number format method - correct me if I am wrong.
Thank you.

Thank you for provided code fixes, we will consider adding them in the main codebase.

You are very welcome.