decimal value addition not working in javascript - javascript

function sum() {
a = Number(document.getElementById("rate_ts").value);
b = Number(document.getElementById("discount").value);
c = a - (Number(a) * Number(b) / 100);
d = Number(document.getElementById("ocharge").value) + c + Number(document.getElementById("pay_amt").value);
tax = (Number(a) * Number(12.36) / 100);
e = tax + d;
document.getElementById("net_amt").value = e;
}
this code is not working right......i want to add tax and for that i have given 12.36 but it is not returning anything..plz help

parseFloat()
Summary
The parseFloat() function parses a string argument and returns a
floating point number.
$('#three').click(function (e) {
a = parseFloat(document.getElementById("rate_ts").value);
b = parseFloat(document.getElementById("discount").value);
console.log(a+' | '+b);
});
WORKING DEMO

Try This,
function sum() {
a = parseFloat(document.getElementById("rate_ts").value);
b = parseFloat(document.getElementById("discount").value);
c = a - (a * b / 100);
d = parseFloat(document.getElementById("ocharge").value) + c + parseFloat(document.getElementById("pay_amt").value);
tax = a * 12.36 / 100;
e = tax + d;
document.getElementById("net_amt").value = e.toFixed(2);
}
If you want only two decimal point value then use number.toFixed(2)
Working Example

Related

Google Sheets NORMDIST( x, mean, standard deviation, cumulative T/F) to JavaScript

Google Sheets has the NORMDIST( x, mean, standard deviation, cumulative T/F) function. I have not been able to find something equivalent in JavaScript. The examples I've found do not allow for all the variables that are in the Google function or I don't have the understanding to implement them.
I am trying to write a reiterative JavaScript function to replace a set of reiterative Google Sheets calculations and need something like this;
x = NORMDIST(x,0,1, TRUE);
y = NORMDIST(x,0,1, FALSE);
At a high-level, there's two things that the NORMDIST function can do: calculate the Normal Distribution's PDF when false is supplied as the last parameter and CDF when true is supplied.
The PDF part is easy: just follow the formula given on Wikipedia for Normal Distribution.
The CDF part is less trivial as it's a non-elementary integral. Your best shot is to find some open-source implementation and translate it into JS. (Preferably a non-iterative one.) It won't be 100% exact due to roundoff errors, but it can get you extremely close. I found one here.
Here's what I came up with:
function _getNDistPDF(x, mean, stdev)
{
const sqrt2PI = Math.SQRT2 * Math.sqrt(Math.PI);
let frac = (x - mean) / stdev;
return Math.exp(-.5 * frac * frac) / (sqrt2PI * stdev);
}
// Adapted from https://people.sc.fsu.edu/~jburkardt/c_src/asa066/alnorm.c
function _getNDistCDF(x, mean, stdev)
{
const a1 = 5.75885480458;
const a2 = 2.62433121679;
const a3 = 5.92885724438;
const b1 = -29.8213557807;
const b2 = 48.6959930692;
const c1 = -0.000000038052;
const c2 = 0.000398064794;
const c3 = -0.151679116635;
const c4 = 4.8385912808;
const c5 = 0.742380924027;
const c6 = 3.99019417011;
const con = 1.28;
const d1 = 1.00000615302;
const d2 = 1.98615381364;
const d3 = 5.29330324926;
const d4 = -15.1508972451;
const d5 = 30.789933034;
const ltone = 7.0;
const p = 0.398942280444;
const q = 0.39990348504;
const r = 0.398942280385;
const utzero = 18.66;
let up = false;
let value;
let y;
// For non-standard NDist
let z = (x - mean) / stdev;
if (z < 0)
{
up = true;
z = -z;
}
if (ltone < z && (!up || utzero < z))
{
value = !up * 1;
return value;
}
y = 0.5 * z * z;
if (z <= con)
{
value = 0.5 - z * (p - q * y
/ (y + a1 + b1
/ (y + a2 + b2
/ (y + a3))));
}
else
{
value = r * Math.exp(-y)
/ (z + c1 + d1
/ (z + c2 + d2
/ (z + c3 + d3
/ (z + c4 + d4
/ (z + c5 + d5
/ (z + c6))))));
}
if (!up)
value = 1 - value;
return value;
}
function NDistJS(x, mean, stdev, cumulative)
{
return cumulative
? _getNDistCDF(x, mean, stdev)
: _getNDistPDF(x, mean, stdev);
}
This gives you a function NDistJS that takes the exact same parameters as NORMDIST.
The PDF is 100% accurate to the Sheets formula.
The CDF is 99.9999992% accurate to the Sheets formula for (x=1, m=0, s=1).

Javascript - Creating number with specific formula modulo11

I created following generator for Czech tax number (IฤŒO). I know that there is certainly better way how to code that in javascript. I am beginner and I would like to see how to write my code properly. The number is created with special formula and it has 8didgits, tha last digit is based on modulo11 as you can see below in code.
Thanks for your replies.
//Generation of single random numbers as variables
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
var c = Math.floor(Math.random() * 10);
var d = Math.floor(Math.random() * 10);
var e = Math.floor(Math.random() * 10);
var f = Math.floor(Math.random() * 10);
var g = Math.floor(Math.random() * 10);
//Formula for tax number
var formula = a * 8 + b * 7 + c * 6 + d * 5 + e * 4 + f * 3 + g * 2;
var modulo11 = formula % 11;
if (modulo11 === 0) {
var h = 1;
} else if (modulo11 === 1) {
var h = 0;
} else {
var h = 11 - modulo11;
};
//Completing tax number
var identificationNumber = "" + a + b + c + d + e + f + g + h;
//displaying number in console
console.log(identificationNumber);
Take benefit from Array data structure to store a, b,...g.
Then "map" this array by (8- indexOfItem) * item
๐Ÿ‘‰๐Ÿผ so , for the 1หขแต— item which has index = 0 , we will have (8 - 0) * a -โžก 8* a.
๐Ÿ‘‰๐Ÿผ for the 2โฟแตˆ item โžก (8 -1) * b โžก 7 *b
๐Ÿ‘‰๐Ÿผ....so on.
Then use "reduce" to calculate the sum .
Then use "join" instead of ""+ a +b + ....+ g+ h
function getH(modulo11) {
if (modulo11 === 0) return 1;
if (modulo11 === 1) return 0;
return 11 - modulo11;
}
//Generation of single random numbers as variables
const numbers= Array.from({length: 7},(v, k) =>Math.floor(Math.random() * 10))
//Formula for tax number
const formula= numbers.map((n, i) => (8 - i) * n).reduce((total, next) => total+ next , 0)// alternative of sum : a * 8 + b * 7 + c * 6 + d * 5 + e * 4 + f * 3 + g * 2
const h= getH(formula % 11);
//Completing tax number
const identificationNumber = [...numbers, h].join('');
//displaying number in console
console.log(identificationNumber);

How to round up the decimals in javascript..? [duplicate]

This question already has answers here:
How to round up a number in Javascript?
(10 answers)
Closed 8 years ago.
In following case I want to round up the decimals. Please tell me how to do that. Now I am getting too many decimals. I wanted only 2 decimals...
<SCRIPT LANGUAGE="LiveScript">
var a=0,b=0,c=0,d=0,e=0,f=0;
function grandtotalall(form){
form.weight8mm.value = form.rods8mm.value * 4.482;
form.amount8mm.value = form.weight8mm.value * 47.7;
a = form.weight8mm.value * 47.7;
form.weight10mm.value = form.rods10mm.value * 6.984;
form.amount10mm.value = form.weight10mm.value * 47.7;
b = form.weight10mm.value * 47.7;
form.weight12mm.value = form.rods12mm.value * 10.254;
form.amount12mm.value = form.weight12mm.value * 47.7;
c = form.weight12mm.value * 47.7;
form.weight16mm.value = form.rods16mm.value * 18.234;
form.amount16mm.value = form.weight16mm.value * 47.7;
d = form.weight16mm.value * 47.7;
form.weight20mm.value = form.rods20mm.value * 28.932;
form.amount20mm.value = form.weight20mm.value * 47.7;
e = form.weight20mm.value * 47.7;
form.weight25mm.value = form.rods25mm.value * 45.888;
form.amount25mm.value = form.weight25mm.value * 47.7;
f = form.weight25mm.value * 47.7;
form.grandamount.value = a + b + c + d + e + f;
}
</SCRIPT>
Please help me out in this..
You can use toFixed(2), eg
form.grandamount.value = form.grandamount.value.toFixed(2);
you can use JavaScript toFixed() Method to round decimal in javascript
var num = 5.56789;
var n = num.toFixed(2);

Polyline Length

I get line length by this functions.
google.maps.LatLng.prototype.kmTo = function(a){
var e = Math, ra = e.PI/180;
var b = this.lat() * ra, c = a.lat() * ra, d = b - c;
var g = this.lng() * ra - a.lng() * ra;
var f = 2 * e.asin(e.sqrt(e.pow(e.sin(d/2), 2) + e.cos(b) * e.cos
(c) * e.pow(e.sin(g/2), 2)));
return f * 6378.137;
}
google.maps.Polyline.prototype.inKm = function(n){
var a = this.getPath(n), len = a.getLength(), dist = 0;
for (var i=0; i < len-1; i++) {
dist += a.getAt(i).kmTo(a.getAt(i+1));
}
return dist;
}
And use :
alert('Line Length: '+ poly1.inKm() +'');
Everything working. But i have a small problem.
Its shows me: >> Line Length: 8.854502612255438km <<
Digits is to long i want it show me only 8.8 how can i do it?
Sorry for my english!
Try something like:
Math.floor(x * 10) / 10
Where x is the number you are trying to show (8.854502612255438).
Instead of floor (which will turn 88.5 to 88) you may want to try round (which will turn 88.5 to 89).
Edit - ah no, that won't work will it because your number is a string with 'km' at the end (did not spot that)...
so... try using parseFloat like this:
Math.floor(parseFloat(x, 10) * 10) / 10
You would have to add 'km' to the end of the string your self, so the full thing becomes:
alert('Line Length: '+ (Math.floor(parseFloat(poly1.inKm(), 10) * 10) / 10) +'km');

Javascript Calculation not working properly

Hello I am trying to do a simple calculation of three values: a + b * c but getting wrong total. If a is 10 and b is 10 it would be 20 multiplied by c which is 2.4. I should get 48 as total. Currently getting 2424.
function compute() {
var a = $('#a').val();
var b = $('#b').val();
var c = $('#c').val();
var total = (a + b) * c;
$('#total').val(total);
}
$('#a, #b, #c').change(compute);
Basic maths : multiplication have precedence over addition.
So in your code, a is additionned to the result of b*c .
Use :
var total = (a + b) * c;
a + b * c is being evaluated as a + (b * c)
What you need is (a + b) * c
Precedence: Brackets > Division > Multiplication > Addition > Subtraction
In your question, you stated that you get 1024. Getting 1024 is impossible. You should get 34. (Check your calculation elsewhere)
a + (b * c) = 10 + (10 * 2.4) = 34
If you want to add a to b BEFORE multiplying, you'll need to use parentheses.
That's because the multiplication oprator has higher precedence than addition.
(a + b) * c
try after parsing the values like:
var total = (parseFloat(a) + parseFloat(b)) * parseFloat(c);
$(document).ready(function() {
function compute() {
var a = $('#a').val();
var b = $('#b').val();
var c = $('#c').val();
var total = (parseInt(a,10) + parseInt(b,10)) * parseFloat(c); alert(total);
$('#total').val(total);
}
$('#a, #b, #c').change(compute);
});
Check DEMO
Your variables are strings. Use parseFloat function.
"10" + "10"*"2.4" = "10"+ 24 = "1024"

Categories