Dot for thousands, comma for decimals and currency after the number
Dot for thousands, comma for decimals and currency after the number
First of all, I'd like to thank all Datatables developers for this splendid tool, it saved me countless hours of programming.
Here in Croatia, there is a different way of showing the price of a product, for example:
1.200,50 kn
which means one thousand kunas and 50 lipas.
So far, I managed to do the following:
Solved sorting with dot for thousands and commas for decimals, but without "kn" after the number (this doesn't work with "kn" after the number), this was set in language settings:
"decimal": ",",
"thousands": "."
Solved sorting with "kn" after number, but with commas for thousands and dot for decimals (this doesn't work with dot for thousands and commas for decimals), with setting custom datatables sort:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"my-currency-pre": function(a) {
return parseFloat(a.replace(/ /gi, ''));
},
"my-currency-asc": function(a,b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"my-currency-desc": function(a,b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
and then in datatable class:
"columnDefs": [
{"sType": "my-currency", "aTargets": [0,1,2,3,4,5,6,7,8,9,10,11,12]}
],
But how do I combine this 2 mutually exclusive features into one feature that allows me to sort the numbers that have dot for thousands, comma for decimals and "kn" after the number?
Thanks for all help in advance.
Answers
Hi,
The
my-currency
plug-in there doesn't take into account what the decimal character has been set as, which is why the two don't just automatically work together.What you would need to do is modify the plug-in so that it will replace the dot's with nothing and the comma with a dot. Do that in the
-pre
function, before theparseFloat
and it will work nicely.Regards,
Allan
Hi Allan,
Unfortunately, I haven't worked enough with javascript to understand your answer fully. Can you please try explaining it with a bit of code?
Thanks in advance for your time, it's greatly appreciated
Hi verzakes,
I had the same problem. The Croatian way of displaying amounts in a currency is exactly the same as the German way. Most countries in Europe do it like this except for this island west of us in the North Sea - and of course Switzerland ... I have two languages on my site for the time being English (UK) and German.
The key difference for sorting is the different date format and the different number format. This is my solution; maybe helpful for you. For date sorting I use moment.js in combination with a data tables plugin. For the (formatted) amount sorting I coded the solution myself (reusing parts of an existing plugin). The advantage for you might be that column detection is fully automated. But you need to know the language the user has chosen. Otherwise it doesn't work. If you have the language you don't need to specify anthying in columnDefs because detection is automatic.
I added the Kunas to the currency array already. Good luck!! And this is the link to the data tables plugin for moment.js: https://datatables.net/plug-ins/sorting/datetime-moment