Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add together numerical values that are stored in strings?

Tags:

javascript

I'm trying to add 1 to my JavaScript variable, but the following code doesn't give expected results:

var val1 = document.getElementById('<%= rng1.ClientID %>');
var val2 = val1.value + "1";
alert(val2.value);

How can I do this?

like image 815
sriramjitendra Avatar asked Feb 04 '11 11:02

sriramjitendra


People also ask

Can numerical values be added to a string object?

In javascript , we can add a number and a number but if we try to add a number and a string then, as addition is not possible, 'concatenation' takes place.

How do you add numbers together in a string in Java?

Adding Numbers and StringsJava uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.

Can we store numeric value in string?

A string represents alphanumeric data. This means that a string can contain many different characters, but they are all considered as if they were text, even if the characters are numbers. A string can also contain spaces.

How many strings can be added to a number field?

The string and the number are presented as one field. Note that you’re not limited to just one string and one number – the CONCAT () function can accept up to 254 arguments (i.e. you can join up to 254 strings/numbers together.

How many arguments can concatenate string and number?

Note that you’re not limited to just one string and one number – the CONCAT () function can accept up to 254 arguments (i.e. you can join up to 254 strings/numbers together. Also note that CONCAT () implicitly converts all arguments to string types before concatenation.

How do I concatenate strings with numbers using T-SQL?

This article presents six ways to concatenate strings with numbers using T-SQL. The most obvious (and possibly the best) way to concatenate a string and a number is to use the CONCAT () function. This allows you to provide the string and the number as two separate arguments. SQL Server will then concatenate them, and your concatenation is complete.

How to sum relative numbers based on their corresponding text strings?

You can use an array formula to sum the numbers based on their corresponding text string within the cell, please do as follows: 1. First you can write down your text strings you want to sum the relative numbers in a column cells. 2.


3 Answers

parseInt($('.scpContainer').height()+200)
like image 90
Saleh Avatar answered Oct 21 '22 14:10

Saleh


you can use the function parseInt() to transform the string stored in val1.value into a number:

var val2 = parseInt(val1.value) + 1;

Note that Number(val1.value) will also convert your value into a number.

-edit-
As yc suggest, is highly recommended to use parseInt(val1.value, 10) + 1; because if your string starts with a "0" the string is interpreted as octal. eg. parseInt("0123") == 83 and not 123

like image 21
Franck Freiburger Avatar answered Oct 21 '22 13:10

Franck Freiburger


The simplest and also preferred way would be to use + to cast to a number.

Examples:

+'234'; // 234
+'042'; // 42! It does not convert to octal

In your case:

var val1 = +(document.getElementById('<%= rng1.ClientID %>').value); // () added just for the looks
var val2 = val1.value + 1; // if val1 is 5, val2 will be 6 hooray!
alert(val2); // just val2, since it's a plain number not an html input element
like image 5
Ivo Wetzel Avatar answered Oct 21 '22 15:10

Ivo Wetzel