Reputation: 2099
In Visual FoxPro, a number can be formatted in a textbox or grid and still be seen by the program as just a number, even though it is shown with commas and period format.
Presently I'm inserting data into a DataTable Jquery as follows:
oTable.fnAddData( ["Bogus data","1,541,512.52","12.5%","0","0","0"]);
But I would like to be entering the data as follows and yet show it with the commas for clarity:
oTable.fnAddData( ["Bogus data",1541512.52,"12.5%","0","0","0"]);
The reason is that when you sort the rows on this column, the character string in the first example will produce a mess. the numbers, hopefully will produce a well ordered list.
If you have any other suggestions on how to fix the sorting of the character number column please suggest it...
TIA
Dennis
Upvotes: 0
Views: 1018
Reputation: 3577
Here's a quick and dirty way to do it;
var number = 1541512.52;
var nicelyformattedNumber = number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
oTable.fnAddData( ["Bogus data", nicelyformattedNumber, "12.5%","0","0","0"]);
Upvotes: 1
Reputation: 11623
A sort plugin for formatted numbers is detailed here, by the author of DataTables:
http://www.datatables.net/plug-ins/sorting
See "formatted numbers":
This plug-in will provide numeric sorting for numeric columns which have extra formatting, such as thousands seperators, currency symobols or any other non-numeric data.
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},
"formatted-num-asc": function ( a, b ) {
return a - b;
},
"formatted-num-desc": function ( a, b ) {
return b - a;
}
} );
Upvotes: 1