How to get the first digit after the decimal point of a float number in Node.js

2 Answers

0 votes
function firstDecimalDigit(f) {
    return f.toString().split('.')[1][0]
}

const f = 891.79234;

console.log(firstDecimalDigit(f));  


/*
run:

7

*/

 



answered Jun 17, 2022 by avibootz
edited Sep 16 by avibootz
0 votes
function firstDecimalDigit(f) {
    return Math.floor(Math.abs(f) * 10)) % 10; 
}

const f = 891.79234;

console.log(firstDecimalDigit(f));  


/*
run:

7

*/

 



answered Jun 17, 2022 by avibootz
edited Sep 16 by avibootz

Related questions

...