How can I calculate the last payment for Reprofiled Amount column with 2 decimal places to make the sum of all payments to be the same as RC amount?

Joined
Oct 24, 2013
Messages
47
Reaction score
0
How can I calculate the last payment for Reprofiled Amount column with 2 decimal places to make the sum of all payments to be the same value as Research Costs amount? The difference needs to be added to the last quarterly payment.

I have 3 payment values stored in numAmounts array. Const numAmounts = [ 106017.96, 106017.96, 106017.96 ]; My code to calculate the last payment gives me 106017.96999999997 but when I pass value to my formatAsCurrency(remainningAmount, 2, 2) I get 106017.96 which if we sum all 3 numAmounts values + remainningAmount there is a difference of 1 cent. That 1 cent needs to be added to the last quarterly payment.
JavaScript:
function formatAsCurrency(numValue, numMinimumDigits, numMaximumDigits)
        {
            try
            {
                if (!numMinimumDigits || isNaN(numMinimumDigits))
                {
                    numMinimumDigits = 0;
                }

                numValue = Number(numValue.toString().replace(/[^0-9.-]/g, ""));
                if (!isNaN(numValue))
                {
                    if (!numMinimumDigits || numMinimumDigits <= 0)
                    {
                        return Number(numValue.toFixed(0)).toString();
                    }
                    else
                    {
                        return numValue.toLocaleString("en-US", { minimumFractionDigits: numMinimumDigits, maximumFractionDigits: numMaximumDigits });
                    }
                }
                else
                {
                    return numValue;
                }
            }
            catch (ex)
            {
                alert("Error in formatAsCurrency(): " + ex.toString());
                disableAll();
                return false;
            }
        }
const RC = 424071.85;    
const numAmounts =  [ 106017.96, 106017.96, 106017.96 ];
const sum = numAmounts.reduce((acc, curr) => acc + curr, 0);
const remainningAmount = RC - sum;
console.log("remainningAmount", formatAsCurrency(remainningAmount, 2, 2));
 
Last edited:
Joined
Jul 4, 2023
Messages
400
Reaction score
44
Did you try use .toFixed in that way

JavaScript:
let remainningAmount = RC - sum;
console.log("remainningAmount", formatAsCurrency(remainningAmount, 2, 2));
remainningAmount = Number(remainningAmount.toFixed(2));
console.log(typeof(remainningAmount), remainningAmount);

or

JavaScript:
const remainningAmount = Number((RC - sum).toFixed(2));
console.log(typeof(remainningAmount), remainningAmount);

or

JavaScript:
const remainningAmount = (RC - sum).toFixed(2);
console.log(typeof(remainningAmount), remainningAmount);
 
Joined
Oct 24, 2013
Messages
47
Reaction score
0
Did you try use .toFixed in that way

JavaScript:
let remainningAmount = RC - sum;
console.log("remainningAmount", formatAsCurrency(remainningAmount, 2, 2));
remainningAmount = Number(remainningAmount.toFixed(2));
console.log(typeof(remainningAmount), remainningAmount);

or

JavaScript:
const remainningAmount = Number((RC - sum).toFixed(2));
console.log(typeof(remainningAmount), remainningAmount);

or

JavaScript:
const remainningAmount = (RC - sum).toFixed(2);
console.log(typeof(remainningAmount), remainningAmount);
Txs. It work. Appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top