Iam trying to do a calculation. When a user selects a dropdown currency. The system calculates and converts the selected currency from the dropdown to INR currency values. The records are coming from mysql db and the record count will be from one record to 300 records. Here the user needs to change each currency from the drop down and the system converts this selected currency value to INR value. Its really a hectic task. So If the user changes one currency value from dropdown, the system should change the entire row currency value to the selected dropdown value and the system should calculate on certain formulae to change the value to INR. All these is working fine.
Now the issue iam facing is to implement the formulae to the jquery.
Here is the fiddle what i have done:
Fiddle
If you can check the fiddle in the jquery part i have used a formula
var newTotal = currency==="INR" ? totalINR : (totalINR * conversionRate / row.find('.inrvalue').val()).toFixed(3);
This above formula wrong:
The actual formula iam looking at is
if (currency==="INR")
{
var newTotal = totalINR;
} else if (currency==="USD")
{
var newTotal = (row.find('.total1.').val() * row.find('.inrvalue').val());
} else {
var newTotal = ((row.find('.inrvalue').val() / conversionRate) * row.find('.total1.').val());
}
How will i change the existing formulae to this? When i implement this formula, iam getting an error Uncaught Error: Syntax error, unrecognized expression: .. Help Appreciated
your function is almost right, you have only one "extra" dot after "total1"
you can safely change
var newTotal = currency==="INR" ? totalINR : (totalINR * conversionRate / row.find('.inrvalue').val()).toFixed(3);
for
var newTotal = 0;
if (currency==="INR")
{
newTotal = totalINR;
} else if (currency==="USD")
{
newTotal = (row.find('.total1').val() * row.find('.inrvalue').val());
} else {
newTotal = ((row.find('.inrvalue').val() / conversionRate) * row.find('.total1').val());
}
hope this helps
Related
I am making a PDF with a custom calculation script and I want to get the product of 2 fields with the results in the format of currency and for the life of me I cant figure it out. I am relatively new to a lot of this.
Here is the code I currently have:
var QtyRow1 = (this.getField( "QtyRow1").value); // the value of QtyRow1;
var CostRow1 = (this.getField( "CostRow1").value); // the value of CostRow1;
var t1 = QtyRow1 * CostRow1; // the value all TotalRows;
if ( t1 < .01 ) {
// t1 will remain blank if total of CostRow1 * QtyRow are less than 1;
event.value = "";
} else {
// otherwise will calculate total of all TotalRows;
event.value = t1;
}
Additionally here is a link to my PDF I am working on from my google drive. I am trying to take the Quantity and Cost of each row and so a total in the format of Currency from the product of the quantity and cost.
So my problem was I wasn't identifying t1 as a number format with in the JavaScript code so when I would go to the format tab under the form field properties and select that I wanted it to formatted to number and currency it wouldn't work cause it would give me an error saying the "t1" hasn't been properly formatted. So all I did was within the if statement made sure to tell JavaScript that t1 needed to be formatted to a number and did so by doing this: event.value = (Number(t1)) as to where before I just had event.value = t1. That fixed it and now everything works great.
Here is a link to the new PDF with the new code in it:
Click Here
// the value of form field QtyRow1;
var QtyRow1 = (this.getField("QtyRow1").value);
// the value of form field CostRow1;
var CostRow1 = (this.getField("CostRow1").value);
// the product of form fields QtyRow1 and CostRow1;
var t1 = QtyRow1 * CostRow1;
// if statement for results
if( t1 < .01 )
{
// t1 will remain blank if total of form fields CostRow1 * QtyRow are less than .01;
event.value = "";
} else {
// otherwise will caclculate the product of form fields QtyRow1 and CostRow1;
event.value = (Number(t1));
}
I have some inputs in a HTML form using javascript to autofill the inputs on changes
The problem is that the follow code show decimals longer decimals like this 42880.34623465464356435634 The code is:
thetotal.value = parseFloat(total.value) * (1.00/1.19);
This code get the price less taxes from the total and it works, but it show me the decimals...
What I need is the price to be in the example:
42880
Without dots or commas . or , like 42.880 or 42,880 and absolutly not 42880.34623465464356435634
I only want to show 42880
Tried with .round like this:
thetotal.value = parseFloat(total.value) * (1.00/1.19).round(2);
.round(2) but for some reason the addEventListener stop working for this input when using .round(2)
user parseInt function for it. Like below :
thetotal.value = parseInt(parseFloat(total.value) * (1.00/1.19));
You will get only Integer as output.
Another way is Math.round. Like it below :
thetotal.value = Math.round(parseFloat(total.value) * (1.00/1.19));
why don't you use Math.round()?
var total = {};
total.value = 51027.6120192;
var thetotal = {};
thetotal.value = Math.round(parseFloat(total.value) * (1.00/1.19));
document.write(thetotal.value);
Output
42880
I need to make a cell text turn red if the value of the cell is equal to the value of the cell two rows above it. I have these two functions in my script editor:
/**
* Compares cell value to another cell relative in position to it.
* Returns true if equal values.
*
* #param {Number} colDiff Relative positioning column difference.
* #param {Number} rowDiff Relative positioning row difference.
*/
function equalsRelativeCellValue(rowDiff, colDiff) {
var thisCell = SpreadsheetApp.getActiveRange();
var relativeCellValue = getRelativeCellValue(rowDiff, colDiff);
if (thisCell.getValue() === relativeCellValue)
return true;
else
return false;
}
/**
* Returns value of cell according to relative position
*
* #param {Number} colDiff Relative positioning column difference.
* #param {Number} rowDiff Relative positioning row difference.
*/
function getRelativeCellValue(rowDiff, colDiff) {
var range = SpreadsheetApp.getActiveRange();
var col = range.getColumn();
var row = range.getRow();
var range2 = SpreadsheetApp.getActiveSheet().getRange(row + rowDiff,col + colDiff);
return range2.getValue();
}
The second function, getRelativeCellValue(rowDiff, colDiff) works just fine, I put 8 in a cell, and two cells below it I entered getRelativeCellValue(-2, 0), and the cell evaluated to 8.
But the first function, getRelativeCellValue(rowDiff, colDiff) won't work as my custom function in conditional formatting for some reason:
Custom formula is: =equalsRelativeCellValue(-2,0)
The difficult part is referring to the value of the cell being referenced in the conditional formatting. But my function looks right to me, it returns true if the cell values are equal, and false if they are not. Im hoping I'm just using the "Custom formula is" feature of Conditional Formatting improperly, but the documentation is pretty sparse.
Just realizer what you want to do, just use conditional formatting, select the range, and apply for the first cell, and it will apply for all correctly:
Eg.
In conditional formating dialog, select Custom Formula, paste the custom formula =J10=J8, and select the range J10:J100.
OldAnswer:
You created a circular reference.
If you input =equalsRelativeCellValue(-2,0) in the the Cell, how can it's value be anything, if it is waiting for the function to resolve?
You can overcome this in a column besides the values, or pass the value directly in the function.
You can also use this to make the between cell have a true/false state:
function equalsRelativeCellValue(rowDiff, colDiff) {
var below = getRelativeCellValue(1, 0);
var relativeCellValue = getRelativeCellValue(2, 0);
if (below === relativeCellValue)
return true;
else
return false;
}
I have 1 select, 2 text inputs & some JSON data in a form:
select input: List of Suppliers
text input 1: Net Amount
text input 2: Gross Amount
JSON Data:contains the rates of various suppliers as JSON in supplier_tax_rates
I am calculating Gross Amount something like this(pseudo code):
grossAmount = NetAmount + ((currently_selected_supplier.tax_percentage_charged / 100) * netAmount)
Here is the complete code:
Calculate total after retriveing tax rate from JSON
Now, this should work but it doesn't. I get NaN(not a number), means something is wrong. But I have trouble find where.
JSfiddle
You have multiple problems in your code. Here is the correct version:
var taxRates = $.parseJSON(supplier_tax_rates);
var getTaxRate = function(id) {
for (var i in taxRates) { // correct loop definition
if (taxRates[i].id == id) { // check you get id correctly
return taxRates[i].tax_percentage_charged; // instead of 'rate'
}
}
};
$('#PurchaseNetAmount').on('change', function(event) {
var taxRatesId = $('#PurchaseSupplierId').val();
var netAmount = parseFloat(this.value);
var grossAmount = netAmount + ((getTaxRate(taxRatesId) / 100) * netAmount);
$('#PurchaseGrossAmount').val(grossAmount);
});
DEMO: http://jsfiddle.net/A9vmg/18/
Your problem is in the look up function.
for(TaxRate in supplier_tax_rates ){
supplier_tax_rates is a string, not a JSON object
Than after you fix that you will have another error
return rate;
What is rate?
Learn to use console.log() or breakpoints so you can step throught your code and debug it.
getTaxRate(taxRatesId) return undefined
I have a page on a site that calculates rate totals for a rental, which are optional. The javascript serves nothing more but to show you the updated total if you select or unselect checkbox options.
Here is what I've tried. It appears to work, but it is adding 2 cents to every other click for one of the checkboxes, I believe because toFixed rounds. How can I keep the number from rounding?
function update_rate(state, amount, field) {
if (state == true) {
rate = parseFloat($('span.rateTotal').text()) + amount;
rate = rate.toFixed(2);
due = parseFloat($('span.'+field).text()) + amount;
due = due.toFixed(2);
$('span.rateTotal').text(rate);
$('span.'+field).text(due);
} else {
rate = parseFloat($('span.rateTotal').text()) - amount;
rate = rate.toFixed(2);
due = parseFloat($('span.'+field).text()) - amount;
due = due.toFixed(2);
$('span.rateTotal').text(rate);
$('span.'+field).text(due);
}
}
Checkbox HTML:
<cfinput type="checkbox" name="linen_service" id="linen_service" checked="checked" value="0" onClick="update_rate(this.checked, #reservationQuery[7].xmlChildren[i]['dblAmount'].xmlText#, 'second-due');" />
Basically passes in the amount of the option, checkbox state, and the name of the field that should be affected.
Edit: Fiddle URL: http://jsfiddle.net/nGrVf/6/
Are you stuck with your your types?
I find it is almost always better to store monetary values in whole numbers of the lowest unit (e.g, cents, or pence) and then add the decimal point two places from the right when displaying.
So the conversions might look like:
var dollars = 100.43;
var cents = Math.round(dollars * 100);
//convert for display
var sCents = String(cents);
var display = sCents.substr(0, sCents.length-2) + "." + sCents.substr(sCents.length-2,2);
alert(display);
Can be seen here at http://jsfiddle.net/jameswiseman76/jhjtP/
Then you don't have to worry about any ugly floating point conversions.
The simplest solution I can think up is to round it down (using muls and floor): i.e.
rate = Math.floor(rate * 100)/100;
Edit: After a bit of testing, I found that there is nothing wrong with the given code. This means the calculation error comes from another part of the code or from the unknown amount variable.