How to get the first digit after the decimal point of a float number in JavaScript

3 Answers

0 votes
function firstDecimalDigit(f) {
    const s = f.toString();
    const pos = s.indexOf('.');
  
    return s[pos + 1];
}

const f = 231.687612;

console.log(firstDecimalDigit(f));  // "6"



/*
run:

6

*/

 



answered Aug 31, 2019 by avibootz
edited Sep 16 by avibootz
0 votes
function firstDecimalDigit(f) {
    return f.toString().split('.')[1][0];
}

const f = 231.687612;

console.log(firstDecimalDigit(f));   // "6"



/*
run:

6

*/

 



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 = 231.687612;

console.log(firstDecimalDigit(f));   // 6



/*
run:

6

*/

 



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

Related questions

...