How do I calculate custom decimal attributes, that represent dollars, in RTC?
I am trying to calculate a total payment from several custom decimal fields, which the user inputs. If I just try to add the values, it appends the values together as if all the fields are strings. If I add the parseInt function?, it works a little better but the values get treated as integer. However, if I have to enter cents (which will be the case occassionally), the cents get dropped, as if they are not there. I'm not really a developer, but used to be ages ago, so I have some concept. I've been looking at other answers to related questions, but haven't found something that works. I'm hoping someone can tell me how to code the javascript to calculate decimals (monetary values). Here's my scripts so far. Note that authorPay, sdiFee, and prompt are all decimal attributes. And I'm returning to a decimal attribute.
dojo.declare("com.impediment.TotalPaymentCalculated", null, {
getValue: function(attribute, workItem, configuration) {
var authPay = parseInt(workItem.getValue("authorPay"));
var SDIfee = parseInt(workItem.getValue("sdiFee"));
var promptPay = parseInt(workItem.getValue("prompt"));
var totalAmt = authPay + SDIfee + promptPay;
return totalAmt;
getValue: function(attribute, workItem, configuration) {
var authPay = parseInt(workItem.getValue("authorPay"));
var SDIfee = parseInt(workItem.getValue("sdiFee"));
var promptPay = parseInt(workItem.getValue("prompt"));
var totalAmt = authPay + SDIfee + promptPay;
return totalAmt;
Accepted answer
Have you tried parseFloat() instead of parseInt()? That should keep the cents values. There is always a way to ensure you keep 2 decimal places using toFixed(2):
var twoPlacedFloat = parseFloat(yourString).toFixed(2)
Comments
Thanks Susan for your quick answer. These suggestions worked!
Revised code:
getValue: function(attribute, workItem, configuration) {
var authPay = 0;
var sdiPay = 0;
var promptPay = 0;
var totalAmt = 0;
authPay = parseFloat(workItem.getValue("authorPay"));
sdiPay = parseFloat(workItem.getValue("sdiFee"));
promptPay = parseFloat(workItem.getValue("prompt"));
totalAmt = authPay + sdiPay + promptPay;
return totalAmt.toFixed(2);
}