相关文章推荐
腼腆的企鹅  ·  java ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The JavaScript function parseInt can be used to force conversion of a given parameter to an integer, whether that parameter is a string, float number, number, etc.

In JavaScript, parseInt(1.2) would yield 1 with no errors, however, in TypeScript, it throws an error during compilation saying:

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

Am I missing something here or is it an expected behaviour from TypeScript?

I don't think it's the correct way. This will produce a float number with no fractional part. But it is not an integer actually. For example you will expect a float number if you divide it by an integer. – Cosmo Jul 27, 2017 at 8:20 The question asks for string | number Math.floor() only takes number so that doesn't work either. One of the other answers suggests Number() which works but perhaps leaves you with a number with decimal places. Math.floor(Number(stringOrNumber)) seems like a sensible option, but also parseInt("" + row.c, 10) seems somewhat acceptable. – Forbesmyester Oct 19, 2017 at 8:45 @Cosmo Your statement is incorrect since JavaScript does not have an integer type. JavaScript only has the type "number", which is an IEEE 754 double precision floating-point number. – Vincent Jan 10, 2022 at 7:44

The function parseInt indeed expects a string in its first argument. Please check the documentation. Usually you can omit the second, radix argument and then it will fall back to the default of 10. But the safest is to always add the numeric system base as second argument (usually 10).

If you'd like to cast a general value to number, you can use the Number function, like this.

var myNumber = Number(myGeneralValue);
                " If this argument is not a string, then it is converted to one using the ToString abstract operation. " Documentation doesn't really describe a required type it should have. the input argument is simply just called "string" for historical reasons.
– jave.web
                Mar 29, 2022 at 7:08

I think other people have already given lots of valid answers here, but in my opinion the easiest approach would be to call .toString() on the original value, and to explicit the radix:

parseInt((1.2).toString(), 10);

@Mahmoud I think the formatter cut the backticks around your template literal. I don't know how to render your syntax in a comment though... – Giorgio Tempesta Aug 8, 2019 at 12:58 Like I said If you add a \ backslash before a tick \`, it keeps the tick inside code formatting – JohannesB Aug 23, 2019 at 9:53

parseInt (string , radix)
The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix.

In normal JS, the first argument is coerced to a string, based on the following rule in the spec:

  • Let inputString be ToString(string).
  • which is why parseInt(1.2) works.

    Note that the spec allows radix to be undefined, which is the same as omitting it, hence the question mark in the radix?: number part of the signature. In this case, of course, it defaults to 10 (unless the string looks like 0xabc).

    As mentioned in other answers, parseInt is not the best solution anyway if what you really want to do is a floor or truncation operation.

    There are different manifestations when negative numbers between 'Math.floor' and 'parseInt'.

    you should use this: 1.2 | 0

    or (1.2).toFixed(0)

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.