3.2.0

howto: format numbers?

I've messed with the setTextFormat function for a number format, but I can't get it to do simple things.

My number is 12345
I want to display it as 12,345

I can only get the comma if I supply a decimal (#,###.##). You would think that dropping the (.##) would get my expected result, but it doesn't. A format of (#,###) displays as 12345,000.

I've searched the forum top to bottom. I see helpful posts on date formatting, but eveyone who has posed the question about numbers goes unanswered.

Can someone point me to a place where all examples of number formatting options are listed?
jeff
August 29,
take a look here if AW doesn't work as you want:

http://www.mredkj.com/javascript/numberFormat.html

Jim Hunter
August 29,
I have the same questions as Jeff. Come on, Alex - help us out!
Mark
August 30,
I found the same problem as jeff too. Here is the solution that I am using

The number format code is
var pattern = /^([^0#]*)([0#]*)([.,]?)([0#]|[0#]{3})([.,])([0#]*)([^0#]*)$/;

I replaced with
var pattern = /^([^0#]*)([0#]*)([.,])([0#]{3}|[0#])([.,]?)([0#]*)([^0#]*)$/

This fixed the problem for me.

The first statement replaces from the end of the string the second replaces from the start of the string.

This code is found in formats/number.js of the source code or
search for "setTextFormat" in the grid.js of the runtime lib.
Greg
October 17,
The problem is with the regional settings on your computer. I havd the same problem and solved it this way:

Compose the format string from your server-side script as:

#<digit grouping symbol>### - for integer numbers
and
#<digit grouping symbol>###<decimal symbol>## - for currency values

in my case it had to be:

"# ###" for integer numbers and "# ###,##" for currency values - without the currency symbol in my case - it just "eats" the space on the screen. Add it if neccessary, but don't hard-code $ in the beginning of the format string as it may be different on your customer's machine... also it may be in the end of the string as well you know :)
Nikolay Simeonov
October 23,
are you applying this to an individual cell or an entire column of data. If it's for an entire column, please show an example. I would be very interested in seeing how to do this.
Jim Shaffer
October 24,
##Update##

The regular expression that I supplied in an early post (Greg - Monday, 17 October, 2005) works as along as you have #,### for your format filter it does not work properly for #,##. So, I found that using the orginal pattern and changing the parsed code works a little better. Again, you will need to change the code in the lib/grid.js or source/lib/format/number.js. Here is the code change for source/lib/format/number.js:

Orginal code
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

        var decimals = f[6].length;


Changed code
var decimals = f[6].length;

        var rs = f[1]; // result start
        var rg = f[3]; // result group separator;
        var rd = (decimals>0)?f[5]:""; // use the result decimal separator if there is a precision specified...
        var re = f[7]; // result end


Now to get 9,000 when the data is 9000 use #,###.

Note, if you do not specify both group and decimal separators it is assumed that only the decimal separator was specified. For example, #,### with 9000 displays 9000,000 Without the code change specified above you will get 9,000. Technically, this is correct but generally the trail decimal point is not need if no places are to be displayed.

Refer to
<a href="http://www.activewidgets.com/active.formats.number/settextformat.html" target="_new">http://www.activewidgets.com/active.formats.number/settextformat.html</a> for reference setTextFormat function.

Greg
December 5,

This topic is archived.

See also:


Back to support forum