This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
Related
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 3 years ago.
how to add two variables, I'm doing it like this and they are concatenating
id('total-quotas').addEventListener('keyup', function () {
id('vl_total').value = (id('total-quotas').value + id('valor6').value);
});
They are concatenating because they are strings.
If you expect those two values to be numeric, then cast them via parseInt.
parseInt(id('total-quotas').value, 10) + parseInt(id('valor6').value, 10)
Are these strings? If they are strings they will contact, if they are integers passed as strings you might need to parse as an integer
See https://www.w3schools.com/jsref/jsref_parseint.asp
This question already has answers here:
Remove commas from the string using JavaScript
(3 answers)
Closed 4 years ago.
How could I transform a number with commas like 123,245 to a number without commas like this 123245, I know that you could already put commas in number without them but how to do that in reverse?
pure JAVASCRIPT please!
var number = Number("123,456".split(",").join(""));
Or:
var number = parseInt("123,456".split(",").join(""));
An alternative is using a regex /,/g
console.log("123,245".replace(/,/g, ''));
console.log("123,245,566".replace(/,/g, ''));
This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 5 years ago.
I have large decimal numbers which I am getting from a request & I want to convert them to string.
So for EG:
I tried all methods converting to string
var r=12311241412412.1241523523523235
r.toString();
r+'';
''+r;
String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'
All methods return the decimal numbers upto 4 digits (12311241412412.1241)
but i want all the number till end.
I also tried r.toFixed().toString() but each time the length of decimal numbers change.
What would be easy way to do this?
the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.
use this instead:
var r = "12311241412412.1241523523523235";
This question already has answers here:
Sum of two input value by jquery
(7 answers)
Closed 7 years ago.
I have three inputs that need to be calculated. I used the .val() method in jQuery to get the value of the input and calculate them with following:
$("document").ready(function(){
$("#calculate").click(function(){
document.getElementById("result").innerHTML="$"+($("#einc").val()+$("#iinc").val()-$("#slint").val());
$("#result").css("display","inline");
});
The first two inputs don't add up, they concatenate; i.e. 5+5=55.
However, the subtraction works fine. So if my first two inputs are 10 and 10 and my last input is 5, it will give me 1010-5=1005.
My expected answer should be 10+10-5=5.
What is wrong with my code?
Because .val() returns string and + also act as the string concatenation operator.
You can convert the string value to a numeric one using parseInt()/parseFloat()/unary operator
$("#result").html("$" + (+$("#einc").val() + (+$("#iinc").val()) - $("#slint").val()));
try this:
Number($("#your_input_field_id").val());
jQuery val() returns string.
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);