how do i convert number starting with decimal to zero

I need to convert number starting with decimal like .15 to 0 rather then 0.15 in javascript. using ParseInt(value) only work for leading zero numbers like 001 or 0.1.

can anyone provide me good solution ??

Answers 1

  • An input value is a String! Trying to use parseInt on a non-number (string decimal missing integer) will result in NaN when the parser tries to perform a string-to-number conversion:

    parseInt(".15", 10)         // NaN
    

    In that case you need to first convert it to a Number:

    parseInt(Number(".15"), 10) // 0
    

    (or using the Unary +)

    parseInt( +".15", 10)       // 0
    

Related Articles