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

2 Answers

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

const f = 895.98731;

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


/*
run:

9

*/

 



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

const f = 895.98731;

console.log(firstDecimalDigit(f));   


/*
run:

9

*/

 



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

Related questions

...