This is my function:
var ans=(X*X)/(Y+Z);
When I enter 10, 20, and 10 - respectively- the addition bit comes out as 2010 and not 30.
How can I fix this?
Make sure to convert your strings to numbers first:
var X = "10";
var Y = "20";
var Z = "10";
X = +X; // unary plus operator converts to a number
Y = Number(Y); // or use the Number function
Z = parseInt(Z, 10); // or parseInt
var ans=(X*X)/(Y+Z);
Related
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 2 years ago.
So I'm having a problem of getting decimals. I can use them but when I do a calculation, that does not work. There's a lot of things to fix but is there any property that can help me?
let rank = prompt('Rank?', ''); // 2000
let x = prompt ('Value of x?', ''); // 90
let y = prompt ('Value of y?', ''); //1.6
var a = parseInt(rank); // 2000
var b = parseInt(x); // 90
var c = parseInt(y); // 1.6
var d = ((2000 - (500 * (3 - c)))/1000); // I get '1' instead of '1.3'
var e = d*b; // I get '90' instead of '117' (1.3*90)
var f = e*(y*y); // 334.0000004 (2*117) instead of 299.52 (2.56*117)
var g = b*(1-c); // 0 (90*(1-1)) instead of -54 (90*(1-1.6))
var h = a/90; // 22.2 is correct (2000/90)
var i = 2-c; // 0 because it rounds 1.6 to 2 (should be 0.4)
var j = h*i; // 0 and it should be 22.2*0.4
I just want to get rounded numbers in decimal part like 22.22222 becomes 22.2.
you want parseFloat, not parseInt
toFixed(1); // if you want rounded to 1 decimal place
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have
var x = 100;
var y = 10;
var b = 10 /100 + 1;
var z = b*50
Expect z = 55. But I got z = 55.0000000001. I don't know why.
How do I fix it in Javascript.
Thanks
Use:
z = parseInt(z);
It will treat z as int.
Use toFixed for digits after the decimal point. Default is 0.
var x = 100;
var y = 10;
var b = 10 /100 + 1;
var z = b*50;
alert(z.toFixed(0));
alert(z.toFixed()); //both are same
For more reference : http://www.w3schools.com/jsref/jsref_tofixed.asp
I made a simple calculator in javascript but the + button doesn't work and it just show the numbers near together
Here is my code:
<script>
function calc(operator) {
var x = document.getElementById("inp1").value;
var y = document.getElementById("inp2").value;
var z = 0;
switch (operator) {
case "+":
z = x + y;
break;
case "-":
z = x - y;
break;
case "*":
z = x * y;
break;
case "/":
z = x / y;
break;
}
document.getElementById("result").innerHTML=z;
}
</script>
You may use like this:
z= +x + +y; // + is prefixed to convert input into number
The x and y variables contain strings. Parse them to numbers:
var x = parseFloat(document.getElementById("inp1").value);
var y = parseFloat(document.getElementById("inp2").value);
It happens to work for the other operators, because there are no subtraction, multiplication or division for strings, it figures out that it has to convert the strings to numbers.
var x=document.getElementById("inp1").value;
var y=document.getElementById("inp2").value;
return you values in those text boxes as strings.
When you use + operator on strings, it will concatenate the values. If you use the same operator on numbers, it will add the values.
You will need to parse the text box values into integer using the parseInt function using one of the following ways.
var x=parseInt(document.getElementById("inp1").value);
var y=parseInt(document.getElementById("inp2").value);
and then do z=x+y; I would recommend this because all the operations, not just addition, will be perfomed on the integers.
or simply change z=z+y; to look like z = parseInt(x) + parseInt(y);
A quick way to convert a string to a number is to use the unary + operator.
z = +x + +y
or
z = parseInt(x) + parseInt(y);
var x = parseInt(document.getElementById("inp1").value);
this converts your "string" number to integer, also you can use parseFloat() if you have float numbers
You may use like this:
var x = document.getElementById("inp1").value*1;
var y = document.getElementById("inp2").value*1;
x in this moment is number!!
More clean for me!!!
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 9 years ago.
I was trying to do some simple mathematical calculations in HTML and jQuery and JavaScript, so I wanted to get input from user.
For input I tried doing this :
var x = prompt("Enter a Value","0");
var y = prompt("Enter a Value", "0");
But I am not able to perform any kind of calculations as these values are strings.
Please, can any one show me how to convert them into integers.
parseInt() or parseFloat() are functions in JavaScript which can help you convert the values into integers or floats respectively.
Syntax:
parseInt(string, radix);
parseFloat(string);
string: the string expression to be parsed as a number.
radix: (optional, but highly encouraged) the base of the numeral system to be used - a number between 2 and 36.
Example:
var x = prompt("Enter a Value", "0");
var y = prompt("Enter a Value", "0");
var num1 = parseInt(x);
var num2 = parseInt(y);
After this you can perform which ever calculations you want on them.
JavaScript will "convert" numeric string to integer, if you perform calculations on it (as JS is weakly typed). But you can convert it yourself using parseInt or parseFloat.
Just remember to put radix in parseInt!
In case of integer inputs:
var x = parseInt(prompt("Enter a Value", "0"), 10);
var y = parseInt(prompt("Enter a Value", "0"), 10);
In case of float:
var x = parseFloat(prompt("Enter a Value", "0"));
var y = parseFloat(prompt("Enter a Value", "0"));
var xInt = parseInt(x)
This will return either the integer value, or NaN.
Read more about parseInt here.
You can use parseInt() but, as mentioned, the radix (base) should be specified:
x = parseInt(x, 10);
y = parseInt(y, 10);
10 means a base-10 number.
See this link for an explanation of why the radix is necessary.
Working Demo Reading more Info
parseInt(x) it will cast it into integer
x = parseInt(x);
x = parseInt(x,10); //the radix is 10 (decimal)
parseFloat(x) it will cast it into float
Working Demo Reading more Info
x = parseFloat(x);
you can directly use prompt
var x = parseInt(prompt("Enter a Number", "1"), 10)
You have to use parseInt() to convert
For eg.
var z = parseInt(x) + parseInt(y);
use parseFloat() if you want to handle float value.
I am facing the issue in division of numbers in java script.
Example:
var x= 2500, var y = 100
alert(x/y)
is showing 25.
I need the answer in 25.00 format. What can I do?
When I divide 2536/100, it gives as expected.
You can try number.toFixed(x)
alert( (x/y).toFixed(2) )
You have to use the toPrecision() method: http://www.w3schools.com/jsref/jsref_toprecision.asp
It's a method defined in Number's prototype.
If you want to dynamically retrieve a float number with a specific precision (in your case 2), you can do de following:
var x = 2500;
var y = 100;
var res = x/y;
var desiredNumberOfDecimals = 2;
var floatRes = res.toPrecision(String(res).length + desiredNumberOfDecimals);
Try doing it this way:
alert((x/y).toFixed(2))
var x = 2500;
var y = 100;
alert( (x/y).toFixed(2) );
is it possible to enter x and y as a dollar amount? ie 25.00 and 1.00? if so then use the parseFloat method.
var x = 25.00
var y = 1.00
alert(parseFloat(x/y));
You need to take a look at number formatting and decimal precision etc.
Look here: http://www.mredkj.com/javascript/nfbasic2.html