It's all about the answers!

Ask a question

How do I calculate custom decimal attributes, that represent dollars, in RTC?

Anne Beville (1323) | asked Jan 14 '15, 4:58 p.m.
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;

Accepted answer

permanent link
Susan Hanson (1.6k2186193) | answered Jan 14 '15, 5:07 p.m.
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)

Anne Beville selected this answer as the correct answer

Anne Beville commented Jan 15 '15, 1:56 p.m.

Thanks Susan for your quick answer. These suggestions worked!

Anne Beville commented Jan 19 '15, 4:19 p.m.

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);

Your answer

Register or to post your answer.