3.0 beta 2

currency format not working

I'm trying to get formatting working with no luck.
My data is being set as: 45.00 and i want it to display as $45.00
my code is:
var currency = new AW.Formats.Number;
currency.setTextFormat("$ #,###.##");
obj.setCellFormat(currency,3);

but it just displays as 45.00

can anyone help?
Jonathan Doklovic
February 6,
Jonathan,

to use formatting you have to assign the data using obj.setCellData() method instead of obj.setCellText(). In this case format.dataToText() method will be called and the cell data will be transformed to the specified format. Alternatively if you use obj.setCellText() then it is treated as ready-to-display text and transformation is not applied (only reverse textToValue() for correct sorting).
Alex (ActiveWidgets)
February 6,
Alex,

I think I found a bug when trying Jonathan's sample above. In my case I didn't want the decimal in the format but leaving it out cause problems.

My CellData is 123456789
Formatting using "#,###" results in: 123456789,000
Formatting using "#,###." results in: 123,456,789.

How do I format so it uses the commas but not the decimal. I want: 123,456,789

Here is some sample code to reproduce. Col 0 shows the unexpected formatting and col 1 works as expected but has the decimal that I don't desire.

<html>
<head>
    <script src="../../runtime/lib/aw.js"></script>
    <link href="../../runtime/styles/xp/aw.css" rel="stylesheet"></link>
</head>
<body>


<script>
    var myData = [
        ["123456789","123456789"],
        ["123456789","123456789"],
        ["123456789","123456789"] ];


    var obj = new AW.UI.Grid;


    /********************************************************************
      Col 0 - Format doesn't work as expected
    ********************************************************************/
        var formatCol_0 = new AW.Formats.Number;
        formatCol_0.setTextFormat("#,###");
        obj.setCellFormat(formatCol_0,0);


    /********************************************************************
      Col 1 - Format works but I had to include unwanted decimal "#,###."
    ********************************************************************/
        var formatCol_1 = new AW.Formats.Number;
        formatCol_1.setTextFormat("#,###.");
        obj.setCellFormat(formatCol_1,1);


    obj.setCellData(myData);	// 2-dimensional js array
    obj.setColumnCount(2);
    obj.setRowCount(3);

    document.write(obj);
</script>

</body>
</html>
Rob Francis
February 6,
Hi Alex,

I emailed a potential source code change to you to consider on this one.
Rob Francis
February 10,
Rob

I'm also wanting to display large integer numbers with comma separators - any chance of sharing your potential fix for the "unwanted decimal" issue which I also experienced.

Many thanks
Will
Will
February 14,
Hi Will,

The fix involves a source code change in Number.js

The line I added is:
if (format.substring(format.length-1,format.length)==rd) { rd="" };

Here is a snapshot:

..
..
..

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;

    if (format.substring(format.length-1,format.length)==rd) { rd="" };

    this._multiplier = Math.pow(10, decimals);

..
..
..

Then to use it you will need to include the period in your format. For example, use "#,###."

The reason you have to declare the period in the format is so AW will know that you are using a comma as the separator and a period as the decimal character (if you want the reverse you would use a format of "#.###,"

Since the decima character is the last thing defined in the format it will not be displayed but it is needed so AW knows your desire for commas or periods.

BTW, this is a source code change which is a pain. I hope Alex will add this to the source code so don't have to remember to do it each time ourselves. I think he is planning to add it.

Rob Francis
February 14,
Yes, this will be done in the next bug fix release.
Alex (ActiveWidgets)
February 14,
Rob, much appreciated.... thanks.
Will
Will
February 14,
Write this code on TextBox's Onblue Event

val=Convert.ToDecimal(TextBox1.Text);
TextBox1.Text=string.Format("{0:c}",val);
Pradnya
November 11,

This topic is archived.

See also:


Back to support forum

Search

Version 2

Looking for AW 2.6 ?
The old site is here.