What is the best way to concatenate numbers and strings?


I’m seeing this code below,

Number(Math.round(value + "e" + decimals) + "e-" + decimals);

And I see that he does not first transpilation occurs without no problems, but in the second he gives error:

Argument of type 'string' is not assignable to parameter of type 'number'.


Number (Math.round (value + "and" + decimals "+" e- "+ decimals); why "and" ?
Instead of “and” more like you want dot “.”


Sorry, that was a mistake on my part.

I’ve already corrected.


Never again will I use + to build strings :smile:


In the suggested article I did not realize that it is indicated never to use, but really there are more adequate solutions and I consider it valid to use the most appropriate and elegant technical format.

But the question remains: “What is the best way to do casting between types in TypeScript, especially Sring for numbers such as ‘5e10’ or ‘5e-10’?”


Didn’t mean to imply that using + is not to be used but with template literals I don’t use that approach anymore. I linked to it because your title question was on concatenating which template literals solves in an elegant way I think. For your error, you’re passing a string instead of number. You can do many things to get past this. Assign number type to the string or actually pass a number by using parseInt or parseFloat to get a valid number from the string.


Well now I understood how the string processing, concatenation and casting in TypeScript works and the problems involved in transpiling between it and JavaScript.

The problem is now corrected and final result is this:

return Number("${${Math.round(Number(${value}e${decimals}))}e-${decimals}}");

Below I present original corrected code.

var strround = Math.round(Number("${value}e${decimals}"));
var strfinal = "${${strround}e-${decimals}}";
return Number(strfinal);

And then I present it dismembered to facilitate the understanding of others who may have the same doubt.

Thanks for help.


Are you wanting to use template literals? If so, use the backtick key, at beginning and end of the string. Your latest sample is using double quotes, ". I might be a little confused on what your exact issue to be honest :slight_smile: maybe a little more context if you still have an issue. Sorry


The problem has been solved, but the case has an example and reading suggestions please be sure to send me.

I am confused when using the double quotes, I tried using backtick but it did not work generating the error below:

app/main-view-model.ts(33,12): error TS2346: Supplied parameters do not match any signature of call target.

app/main-view-model.ts(33,22): error TS2304: Cannot find name '$'.
app/main-view-model.ts(33,23): error TS1005: '}' expected.
app/main-view-model.ts(33,28): error TS1005: ':' expected.
app/main-view-model.ts(33,35): error TS2346: Supplied parameters do not match any signature of call target.
app/main-view-model.ts(33,42): error TS2304: Cannot find name '$'.
app/main-view-model.ts(33,43): error TS1005: ',' expected.
app/main-view-model.ts(33,50): error TS1005: ',' expected.
app/main-view-model.ts(33,50): error TS2304: Cannot find name 'e$'.
app/main-view-model.ts(33,52): error TS1005: ',' expected.
app/main-view-model.ts(33,65): error TS1005: ',' expected.
app/main-view-model.ts(33,65): error TS2304: Cannot find name 'e'.
app/main-view-model.ts(33,67): error TS2304: Cannot find name '$'.
app/main-view-model.ts(33,68): error TS1005: ',' expected.
app/main-view-model.ts(33,78): error TS1005: ',' expected.
app/main-view-model.ts(33,79): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
app/main-view-model.ts(159,2): error TS1160: Unterminated template literal.

TypeScript compiler failed with exit code 1

Thank you for your help.


What does line 33 of main-view-model.ts look like now? You shouldn’t get TS errors with template literals unless you have an error in the code so would need to see it to point out what’s off :smile:

The last warning indicates you didn’t close a template literal, so maybe you missed the backtick to close the literal and that’s the issue you have now.


I rewrite the code now, and actually is this,

private round(value: number, decimals: number) {
    var strnumber = `${value}e${decimals}`;
    var numround = Math.round(Number(strnumber));
    var strfinal = `${numround}e-${decimals}`;
   return Number(strfinal);

I transpile and not get errors.