Jump to content

Total * Markup * Tax in JavaScript


Recommended Posts

If someone could help me with a couple of things:

1). When the page first loads, all of the Total fields start with NaN. Is there a way I can start them out as blank, or 0? I tried putting in a placeholder, but it still doesnt work.

2.) I cannot seem to figure out how to get the currency format to work on these Total fields. The SubTotal ends with 1 digit after the decimal point, and the County Tax Total often has many digits after the decimal. Is there a way I can format all of these Total fields as currency? They are already set as Currency DataType and I have the app localization set for currency as well, to no effect

Thanks again.

//Virtual 13 is the new subtotal after calculations

document.addEventListener('DataPageReady', function (event) {
function calculate() {
    var subTotal = parseFloat(document.getElementById("InsertRecordSubtotal").value);
    var markUp = parseFloat(document.getElementById("InsertRecordMarkUp").value);
    var countyTax = parseFloat(document.getElementById("InsertRecordCounty_Tax").value);
    var miscPerc = parseFloat(document.getElementById("InsertRecordMisc_Percent").value);
    var freight = 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("cbParamVirtual13").value = (subTotal) + (markUpTotal) + (countyTaxTotal) + (miscPercTotal) + (freight);
 
 }
 setInterval(calculate, 1500);
});

 

 

Link to comment
Share on other sites

  • 2 weeks later...

@kpcollier,

You can use the following JS code if you decide to use a custom code.

<script type="text/javascript">

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("cbParamVirtual1").value = (subTotal) + (markUpTotal) + (countyTaxTotal) + (miscPercTotal) + (freight);
 
 }
 
setInterval(calculate, 1500);

</script>

Regards,

vitalikssssss

Link to comment
Share on other sites

2 hours ago, Vitalikssssss said:

Hi @kpcollier,

Why do not you use Calculated value to perform such calculation?

You can use isNull function in Calculated value in order to replace null with zero.


IsNull([@field:Number], -0)

Hope this helps.

Regards,

vitalikssssss

I was originally using calculated fields for this page, but because I have so many of them, it made the loading speed incredibly slow. This is used for estimating so I need a ton of dropdowns and calculations on a single form.

Thank you for the updated code - I greatly appreciate it!

Link to comment
Share on other sites

  • 2 weeks later...

If anyone could help me out with trying to format this, it would be great! ALL of my JS calculations are coming out as just a number. (i.e. '12345.678' instead of '12,345.68'). I think it is because I am manipulating the numbers through the js script above, so it is just ignoring the formatting options. I am not sure. All of the Totals fields are currency datatype, but they still show up as just a number. If there is anyway I can format it as USD currency, that would be great.

TIA

Link to comment
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...