开发者

How to concatenate two numbers in javascript?

I'd 开发者_高级运维like for something like 5 + 6 to return "56" instead of 11.


Use "" + 5 + 6 to force it to strings. This works with numerical variables too:

var a = 5;
var b = 6;
console.log("" + a + b);


You can now make use of ES6 template literals.

const numbersAsString = `${5}${6}`;
console.log(numbersAsString); // Outputs 56

Or, if you have variables:

const someNumber = 5;
const someOtherNumber = 6;
const numbersAsString = `${someNumber}${someOtherNumber}`;

console.log(numbersAsString); // Outputs 56

Personally I find the new syntax much clearer, albeit slightly more verbose.


var value = "" + 5 + 6;
alert(value);


just use:

5 + "" + 6


I know this is an old post and has been answered many times. I too was wondering if JavaScript had a function that would do this. I was doing some math programming and needed to concatenate two numbers.

So the what if I needed to combine two numbers 17 and 29. Sure I can turn them into strings and concatenate them then turn the new string back into a number. That seems to work pretty well and I can go on with my code, but lets take a look here and try to figure out what is really happening here.

What are we doing to these two numbers, how do we take 17 and 29 and turn it into one thousand seven hundred and twenty-nine? Well we can multiply 17 by 100 then add 29. And how about 172 and 293 to get one hundred seventy-two thousand two hundred and ninety-three? Multiply 172 by 1000 and add 293. But what about only 2 and 9? Multiply 2 by 10 then add 9 to get 29.

So hopefully by now a pattern should be apparent to you. We can devise a math formula to do this calculation for us rather than just using strings. To concatenate any two numbers, a and b, we need to take the product of a and 10 to the power of length b then add b.

So how do we get the length of number b? Well, we could turn b into a string and get the length property of it.

    a * Math.pow(10, new String(b).length) + b;

But there has to be a better way to do this without strings, right? Yes there is.

How to concatenate two numbers in javascript?

For any two numbers, a and b, with any base B. We are going to multiply a by base B to the power of length b (using log base of b then flooring it to get the nearest whole number then adding 1 to it) then adding b.

So now our code looks like this:

    a * Math.pow(10, Math.floor(Math.log10(b)) + 1) + b;

But wait, what if I wanted to do this in base 2 or base 8? How can I do that? We can't use our formula that we just created with any other base but base 10. The JavaScript Math object already has built-in functions for base 10 and 2 (just Math.log), but how do we get log functions for any other base? We divide the log of b by the log of base. Math.log(b) / Math.log(base).

So now we have our fully functioning math based code for concatenating two numbers:

    function concatenate(a, b, base) {
        return a * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
    }
    var a = 17, var b = 29;
    var concatenatedNumber = concatenate(a, b, 10);
    // concatenatedNumber = 1729

If you knew you were only going to be doing base 10 math, you could add a check for base is undefined then set base = 10:

    function concatenate(a, b, base) {
        if(typeof base == 'undefined') {
            base = 10;
        }
        return a * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
    }

    var a = 17, b = 29;
    var newNumber = concatenate(a, b); // notice I did not use the base argument
    // newNumber = 1729

To make it easier for me, I used the prototype to add the function to the Number object:

    Number.prototype.concatenate = function(b, base) {
        if(typeof base == 'undefined') {
                base = 10;
        }
        return this * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
    };
    var a = 17;
    var newNumber = a.concatenate(29);
    // newNumber = 1729


simple answer:

5 + '' + 6;


var output = 5 + '' + 6;


Use

var value = "" + 5 + 6;

to force it to strings.


You can also use toString function to convert it to string and concatenate.

var a = 5;
var b = 6;
var value = a.toString() + b.toString();


Another possibility could be this:

var concat = String(5) + String(6);


// enter code here
var a = 9821099923;
var b = 91;
alert ("" + b + a); 
// after concating , result is 919821099923 but its is now converted into string  

console.log(Number.isInteger("" + b + a))  // false

// you have to do something like this

var c= parseInt("" + b + a)
console.log(c); // 919821099923
console.log(Number.isInteger(c)) // true


This is the easy way to do this

var value = 5 + "" + 6;


I'd prefer the concat way :

const numVar1 = 5;
const numVar2 = 6;
const value = "".concat(numVar1, numVar2);
// or directly with values
const value2 = "".concat(5, 6);

You can also use an array which can help in some use cases :

const value3 = [5, 6, numVar1].join('');


I converted back to number like this:

const timeNow = '' + 12 + 45;
const openTime = parseInt(timeNow, 10);

outputs

1245


To add to all answers above I want the share the background logic:

Plus is an addition operator that is also used for concatenation of strings. When we want to concatenate numbers. It should be the understanding that we want to concatenate the strings, as the concatenation of numbers doesn't make valid use cases to me.

We can achieve it in multiple ways,

Through type conversion

let a = 5;
a.toString()+5 // Output 55 type "string"

This will also work and doing type conversion in the background,

5 +""+ 5 // Output 55 type "string"

If you are determined to concatenate two string and type of output should be int, parseInt() works here

parseInt(5 +""+ 5)  //Output 55 Type "number"


You can return a number by using this trick:
not recommended

[a] + b - 0

Example :

let output = [5] + 6 - 0;
console.log(output); // 56
console.log(typeof output); // number
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜