How do you ensure that the JavaScript number 1. 0 is still 1. 0 after converting it into a string?

A headache. I want to match a positive integer, but it doesn"t include something like 1.0, so I want to convert a string first, and then go to the matching point regularly. as a result, if any form of conversion fails, 1.0 will be converted to 1. and directly use 1.0 numbers to regular match, it can not match to the point, as if the inside is first converted into a string and then matched.

has anyone ever encountered a similar demand? No matter how broken it is, you can"t use a function like toFixed, because the number may not be a decimal.

Mar.18,2021

Math.floor (num) = = num is fine, but 1.0 cannot be excluded, nor can it be excluded, because the number 1.0 and the integer 1 in js are the same thing. For strings, use assertions


I seldom use floating-point operations myself, and there is a problem with the accuracy of floating-point numbers. I always mark it with integers, and if it's a floating-point number, I divide it by 100, and the last two places are decimals.


. Is the source data number or string ?

  • number : Number.isInteger
  • string : regular expression

or do you want to match the literal quantity? You can only feed the entire js file to the regular expression. But the bizarre need of matching literal quantity is unheard of, and if it is assigned by the boss, job-hopping is recommended.

Menu