I am experiencing some difficulty in working with Javascript and Plumsail forms for SharePoint Online. On my form I want to calculate a total for various fields the user will enter amount into. In our SPO tenant the amounts are formatted using a comma as the decimal separator, which requires additional helper functions to convert the values to numbers before it can be used in a calculation. Our users uses a dot as the decimal separator when entering amounts. I am wondering if I am missing is a simpler or better solution to deal with these issues?
This is my JavaScript code
fd.spBeforeSave(function() {
var formatter = new Intl.NumberFormat(fd.culture, {
style: 'decimal',
minimumFractionDigits: 2,
});
function formatNumber(value) {
return formatter.format(kendo.parseFloat(value, fd.culture));
};
fd.field('Cash_x0020_Tender').value = formatNumber(fd.field('Cash_x0020_Tender').value);
fd.field('Sales_x0020_Declared').value = formatNumber(fd.field('Sales_x0020_Declared').value);
});
fd.spRendered(function() {
fd.field('Total_x0020_Tender').disabled = true;
var formatter = new Intl.NumberFormat(fd.culture, {
style: 'decimal',
minimumFractionDigits: 2,
});
fd.field('Sales_x0020_Declared').$on('change', function() {
calculateTotals();
});
fd.field('Cash_x0020_Tender').$on('change', function() {
calculateTotals();
});
fd.field('Card_x0020_Tender').$on('change', function() {
calculateTotals();
});
function calculateTotals() {
var totalTender = 0;
totalTender = castNumber(fd.field('Cash_x0020_Tender').value)
+ castNumber(fd.field('Card_x0020_Tender').value);
fd.field('Total_x0020_Tender').value = formatNumber(totalTender);
}
function castNumber(value) {
return kendo.parseFloat(value, fd.culture)
}
function formatNumber(value) {
return formatter.format(castNumber(value));
}});
Below is a sample of the form. Note that I need to format any amounts entered before the form is saved or I get an error stating ‘Only numbers can go here’. I also noticed that, although the SPO field is defined as a number, the browser renders the field with input type as text.