Jump to content
kpcollier

Format Currency With Custom Calculations

Recommended Posts

I have tried a couple of different solutions from posts found here and here. But, for some reason, I cannot get it to work for me. 

I've tried multiple different ways and nothing works. I don't get any errors in the console with the provided solutions, but it was not working at all. I have a feeling it has something to do with the setInterval, where it just doesn't get a chance to work, however I have tried nesting the functions and it might really just be my lack of JS knowledge and something easy. 

Here is one part of the JS that I can't get to format. There are multiple other parts, but I feel if this one can get working, I can fix the others.

function calculate() {

    var subTotal = isNaN(parseFloat(document.getElementById("InsertRecordSubtotal").value)) ? 0 : parseFloat(document.getElementById("InsertRecordSubtotal").value);

    var markUp = isNaN(parseFloat(document.getElementById("InsertRecordMarkUp").value)) ? 0 : parseFloat(document.getElementById("InsertRecordMarkUp").value) ;

    var countyTax = isNaN(parseFloat(document.getElementById("InsertRecordCounty_Tax").value)) ? 0 : parseFloat(document.getElementById("InsertRecordCounty_Tax").value);

    var miscPerc = isNaN(parseFloat(document.getElementById("InsertRecordMisc_Percent").value)) ? 0 : parseFloat(document.getElementById("InsertRecordMisc_Percent").value);

    var freight = isNaN(parseFloat(document.getElementById("InsertRecordFreight_Total").value)) ? 0 : parseFloat(document.getElementById("InsertRecordFreight_Total").value);


    var markUpTotal = document.getElementById("InsertRecordMarkUp_Total").value = (subTotal) * (markUp);

    var countyTaxTotal = document.getElementById("InsertRecordCountyTax_Total").value = (subTotal) * (countyTax);

    var miscPercTotal = document.getElementById("InsertRecordMisc_Percent_Total").value = (subTotal) * (miscPerc);

    document.getElementById("InsertRecordNew_Subtotal").value = (subTotal) + (markUpTotal) + (countyTaxTotal) + (miscPercTotal) + (freight);
 
 }

 
setInterval(calculate, 1500);

This is on a submission form. I am not using calculated fields because the amount of calculations I have made every interaction take 2+ seconds to register.

Thanks for the help!

Share this post


Link to post
Share on other sites

Hi @kpcollier,

Try the following JavaScript:

<script type="text/javascript">

function formatAsDollars(el) {
el.value = el.value.replace(/[^\d]/g,'').replace(/(\d\d?)$/,'$1').replace(/^0+/,'').replace( /\d{1,3}(?=(\d{3})+(?!\d))/g , "$&,");
el.value = el.value ? '$' + el.value : '';
}

var fields = ["Subtotal", "MarkUp", "County_Tax", "Misc_Percent", "Freight_Total"]; //specify your fields here

fields.forEach(element => {
    element = "InsertRecord" + element; // replace "InsertRecord" with "EditRecord" for Details/Single Record Update DP 
    document.getElementById(element).onkeyup = function() {
        formatAsDollars(this);
    }
    document.getElementById(element).onchange= function() {
        formatAsDollars(this);
    }
});

function calculate() {

    var subTotal = document.getElementById("InsertRecordSubtotal").value.length == 0 ? 0 : (parseFloat(document.getElementById("InsertRecordSubtotal").value.replace(/[$,]+/g,"")));
    var markUp = document.getElementById("InsertRecordMarkUp").value.length == 0 ? 0 : (parseFloat(document.getElementById("InsertRecordMarkUp").value.replace(/[$,]+/g,"")));
    var countyTax = document.getElementById("InsertRecordCounty_Tax").value.length == 0 ? 0 : (parseFloat(document.getElementById("InsertRecordCounty_Tax").value.replace(/[$,]+/g,"")));
    var miscPerc = document.getElementById("InsertRecordMisc_Percent").value.length == 0 ? 0 : (parseFloat(document.getElementById("InsertRecordMisc_Percent").value.replace(/[$,]+/g,"")));
    var freight = document.getElementById("InsertRecordFreight_Total").value.length == 0 ? 0 : (parseFloat(document.getElementById("InsertRecordFreight_Total").value.replace(/[$,]+/g,"")));


    var markUpTotal = document.getElementById("InsertRecordMarkUp_Total").value = (subTotal) * (markUp);
    
    var countyTaxTotal = document.getElementById("InsertRecordCountyTax_Total").value = (subTotal) * (countyTax);
    
    var miscPercTotal = document.getElementById("InsertRecordMisc_Percent_Total").value = (subTotal) * (miscPerc);
    
    var total = document.getElementById("cbParamVirtual1").value = (subTotal) + (markUpTotal) + (countyTaxTotal) + (miscPercTotal) + (freight);
  
    var dp_el = [
        document.getElementById("InsertRecordMarkUp_Total"),
        document.getElementById("InsertRecordCountyTax_Total"),
        document.getElementById("InsertRecordMisc_Percent_Total"),
        document.getElementById("cbParamVirtual1")
    ];

    dp_el.forEach(element => formatAsDollars(element));

 }
 
setInterval(calculate, 1500);


</script>

Regards,

vitalikssssss

Share this post


Link to post
Share on other sites

Thank you @Vitalikssssss, I appreciate it! 

I have tried your script. It gets the Subtotal, but it won't allow you to select/input a value for Mark Up, Tax, Misc, and Freight. They are all dropdowns, beside freight which is a text field. And it would also not take in cents. 

I tried messing with some stuff to get it to work, no luck yet. I will update this post when I find the solution.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...