Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove leading 0 before decimal point and return as a number- javascript

Problem

I need to return a number in the format of .66 (from an entered value which includes the leading zero, e.g. 0.66)

It must be returned as an integer with the decimal point as the first character.

what method in JavaScript can help me do this?

What have I tried?

I've tried converting it toString() and back to parseInt() but including the decimal point makes it return NaN.

I've tried adding various radix (10, 16) to my parseInt() - also unsuccessful

Sample Code

const value = 0.66;

if(value < 1) {
    let str = value.toString().replace(/^0+/, '');
    // correctly gets '.66'
    return parseInt(str)
}

//result NaN

Expectations

I expect an output of the value with the leading 0 removed e.g. 0.45 --> .45 or 0.879 --> .879

Current Observations

Output is NaN

like image 597
asking for a friend Avatar asked Dec 19 '25 03:12

asking for a friend


1 Answers

I tried a quick solution, you may try to do this:

let a = 0.45;
// split on decimal, at index 1 you will find the number after decimal
let b = a.toString().split('.')[1];
like image 185
Lata Tiwari Avatar answered Dec 21 '25 19:12

Lata Tiwari