3.0 beta 2

Problem with AW.Formats.Number

Hello, we are having some problems formatting numbers using AW.Formats.Number.

We want a format with 6 decimal positions, so we are using the following format:
var decimal_6decFormat = new AW.Formats.Number; 
       decimal_6decFormat.setTextFormat("###.######");


It works fine most of the time, but when the value is 0, then the grid shows: “5e-7”. We have traced the code and the problem seems to be in the function doFormat:

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);
            };



Any idea? Because our client only wants it with the format 0.000000…

In addition, it is not only a matter of format, because if we send 5e-7, we are storing 0.0000005 which is not 0 (and for this application, precision is very important)

Thanks in advance,

José Luis.

Jose Luis (Spain)
April 6,
Here is a fix for AW.Formats.Number which corrects this problem. The modified code also allows more than 6 digits after the decimal point and also allows 0 decimals using formats like #. or #,###.

Add this code at the end of aw.js -

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("");
};
Alex (ActiveWidgets)
April 6,
Thanks Alex, it works great! ;)
Jose Luis (Spain)
April 7,
Hi, we are having problem formatting numbers with AW.Formats.Number.
We want a format sth like this (xxx,xxx,xxx.xx) for atleast 9 digits, i have tried various formats here but noone helped:

var formatObj = new AW.Formats.Number;
formatObj.setTextFormat("$#,###.##");
// formatObj.setTextFormat("$###,###.##");
// formatObj.setTextFormat("$###,###,###.##");
// formatObj.setTextFormat("$###.##");

It always give the same output, 1,101.90 for 452101.90
1,152.66 for 626,152.66, etc.

What i figure out is this:

this.dataToText = function (v) {
return doFormat.call(this, Number(("" + v).replace(numPattern, "")));
};

This function returns false value.

Any Idea to solve this kinda problem. Thanks in advance

Mukesh Singla
May 17,
i think you're using variable v for a second time somewhere... or numPattern - it's most common mistake or error (choose one ;]). if not these it must be something with other variables and it's naming! ;]
konrad
July 31,

This topic is archived.

See also:


Back to support forum

Search

Version 2

Looking for AW 2.6 ?
The old site is here.