JS subtracting numbers when one has decimal and does not - javascript

I have two vars that I need to subtract from one another.
My code is
var discountedTotal = totalAmount - discountAmount;
totalAmount is static and that value is 3,916.64
discountAmount is entered via an input and is a round number i.e. 500
so discountedTotal = 3,916.64 - 500;
I am getting a result of NAN, what is the best way to handle this scenario? Thanks

var totalAmount = '3,916.64';
var discountAmount = 500;
totalAmount = parseFloat(totalAmount.replace(',',''));
console.log(totalAmount);
discountedTotal = totalAmount - discountAmount;
console.log(discountedTotal);
Try this. Hope it helps.

Related

Add percent to numbers

How can I add percent to a sum? I have tried var sum = 3.25 + '3.4%'; but it didn't work. I'm just getting 0.00 as an answer.
To "add a percent to a number" means "multiply the number by (1 + pct)":
var sum = 3.25;
sum = sum * (1 + 0.034);
You could equivalently skip the 1 (that's just the way I think about it) and add:
var sum = 3.25;
sum += sum * 0.034;
So if you're starting off with a string representation of a percentage, you can use parseFloat() to make it a number:
var pct = "3.4%"; // or from an <input> field or whatever
pct = parseFloat(pct) / 100;
The parseFloat() function conveniently ignores trailing non-numeric stuff like the "%" sign. Usually that's kind-of a problem, but in this case it saves the step of sanitizing the string.
The simplest method is to use arithmetic operators.
var sum = 3.25
sum += sum *= 0.034;
< 3.3605
String concatenation and number adding using the same + symbol. You need to use () around the numbers.
var sum = (3.25+3.4)+"%";
let's assume that you want to add x percent to the current number :
const percentage = 14.5; let number = 100 const numberPlusPercentage = number + number / 100 * percentage
console.log('result = 'numberPlusPercentage.toFixed(2))
result = 114.5

Javascript - X is what percent of X function/equation

My script dynamically produces a number and this can range from anything between 1-278.
I then need to do a number is what percent of 603.
Say for example that number turns out to be 67, i then need to know what percent 67 is of 603.
The function should return 11.1..%
Basically the second option on this page:
http://www.percentagecalculator.net/
Can i do this with javascript?
var total = 603;
var num = 67 // what have been created dynamically beforehand
num is what percent of total?
Well, you just do :
percentage = (num/total)*100;
Here you go:
var total = 603;
var num = 67 /*whatever was generated before*/;
var percentage = num / total * 100;
here is a jsfiddle to demonstrate my solution: http://jsfiddle.net/zfhkB/2/
Hope I helped :)

Javascript calculation not giving desired output

I have the following code:
function calculateVat() {
var vat = null;var amount=null;
vat = document.getElementById("hiddenvat").value;
amount = document.getElementById("numtxt_itemCost").value;
var total = parseInt(amount)* parseInt(vat) / 100 ;
document.getElementById("txt_Total_text").value = total;
}
Here I am calculating VAT price according to amount price
E.G. VAT value is 13.5
If I input 1780 on numtxt_itemCost Field then it should give the output 240.30,
but when I run this programme by putting value 1780 on numtxt_itemCost field
it shows the 231.4 which is the wrong output,
You should use parseFloat instead:
parseFloat(1780)* parseFloat(13.5) / 100 //240.3
when you use parseInt, all decimals get stripped off.
Try this:
function calculateVat() {
var vat = null;var amount=null;
vat = document.getElementById("hiddenvat").value;
amount = document.getElementById("numtxt_itemCost").value;
var total = parseInt(amount)* parseFloat(vat) / 100 ;
document.getElementById("txt_Total_text").value = total;
}
The problem is that you were converting the vat to an "int" using parseInt(vat), so the answer you were seeing was the result of a 13% vat rather than 13.5%.

Electricity units calculator

I'm basically using functions and if else if statements to build an electricity reading calculator.
The units given is 1236 which is a parameter of the function called elecReading. This will be used as the amount of units used and it will calculate the amount that must be paid.
However, the first 0-500 units are billed at $1 per unit. The next 500-1000 units are billed at $1.10 a unit, and over 1000 units are billed at $3.20 a unit. For example, if I used 1000 units, my bill would be $1050.
I'm unsure how I can get this working without breaking down 1236 into singular numbers manually. How can I write a calculator like this with JavaScript?
Obviously I'm not asking for the complete answer, but a push in the right direction would be very helpful at this stage!
Thanks for the help in advance
The static version would be something like:
var UNIT_PRICE_1001_OVER = 3.20;
var UNIT_PRICE_501_1000 = 1.10;
var UNIT_PRICE_UNDER_500 = 1.00;
function elecReading(units) {
var price = 0;
if (units > 1000) {
price += (units-1000) * UNIT_PRICE_1001_OVER;
units = 1000;
}
if (units > 500) {
price += (units - 500) * UNIT_PRICE_501_1000;
units = 500;
}
price += units * UNIT_PRICE_UNDER_500;
return price;
}
This is assuming the unit price ranges are 1-500, 501-1000, 1001-Inf. Obviously this can be done more generally / with less hardcoding, using a list of objects representing a price range + price per unit in said range.
Try following function. Assuming peak rates are from units 1000+, medium rates from 501-1000, and offpeak rates from 0-500.
You could change the variables names as per your requirements/understanding.
EDITED:
While loop is added to keep reducing total units until they are greater than 1000
function elecReading(units){
var totalUnits=units;
var offPeakRate=1;
var mediumRate=1.10;
var peakRate=3.20;
var totalCharges=0;
if(totalUnits>1000){
PeakUnits = totalUnits-1000;
totalCharges = totalCharges + (PeakUnits * peakRate);
totalUnits = 1000;
}
if(totalUnits > 500){
totalUnits = totalUnits-500;
totalCharges = totalCharges + (totalUnits * mediumRate);
}
totalCharges = totalCharges + (totalUnits * offPeakRate);
return totalCharges;
}
console.log(elecReading(2000));

Javascript/Jquery : Mathematically Divide Two Variables

Here's my code:
var frameWidth = 400;
var imageWidth = $('#inner-image').css('width');
var numberOfFrames = imageWidth/frameWidth;
How do I make "numberOfFrames" display as a quotient? I.E. process "frameWidth" and "imageWidth" as numbers, rather than objects?
Let me know if I need to explain myself more clearly. Thanks!
.css('width') is likely returning the value with px. You can use parseInt() to get only the number.
var frameWidth = 400;
var imageWidth = parseInt( $('#inner-image').css('width'), 10);
var numberOfFrames = imageWidth/frameWidth;
The second argument 10 specifies the base that parseInt() should use.
You can also use the width()(docs) method to get the result without the px.
var frameWidth = 400;
var imageWidth = +$('#inner-image').width();
var numberOfFrames = imageWidth/frameWidth;
Here I used the unary + operator to make it a Number instead of a String.

Categories

Resources