Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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;

1

1 vote


Accepted answer

Permanent link
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

1 vote

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

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 12,019

Question asked: Jan 14 '15, 4:58 p.m.

Question was seen: 3,359 times

Last updated: Jan 19 '15, 4:19 p.m.

Confirmation Cancel Confirm