Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,781 answers

573 users

How to get the Nth digit after the decimal point of a floating-point number in JavaScript

3 Answers

0 votes
const f = 231.687312;

const N = 3;

console.log(f.toString().split('.')[1][N - 1]); 
        
  
  
     
/*
run:
      
"7"
         
*/

 



answered Jun 17, 2022 by avibootz
0 votes
/**
 * Returns the Nth digit after the decimal point.
 *
 * Examples:
 * getNthDecimalDigit(12.3456, 1) => 3
 * getNthDecimalDigit(12.3456, 2) => 4
 * getNthDecimalDigit(12.3456, 3) => 5
 * getNthDecimalDigit(12.3456, 4) => 6
 *
 * @param {number} value - The number to inspect.
 * @param {number} position - The digit position after the decimal point (1-based).
 * @returns {number} The Nth digit, or 0 if it does not exist.
 * @throws {TypeError} If arguments are invalid.
 */
function getNthDecimalDigit(value, position) {
    // Validate inputs
    if (!Number.isFinite(value)) {
        throw new TypeError("value must be a finite number.");
    }

    if (!Number.isInteger(position) || position < 1) {
        throw new TypeError("position must be a positive integer.");
    }

    // Convert to absolute value so negative numbers work correctly
    const absValue = Math.abs(value);

    // Shift the desired digit to the ones place
    const shifted = absValue * Math.pow(10, position);

    // Extract the digit
    return Math.floor(shifted) % 10;
}

// -----------------------------------------------------------------------------
// Usage Examples
// -----------------------------------------------------------------------------

const number = 123.456789;

console.log(`Number: ${number}`);

console.log(`1st digit after decimal: ${getNthDecimalDigit(number, 1)}`);
console.log(`2nd digit after decimal: ${getNthDecimalDigit(number, 2)}`);
console.log(`3rd digit after decimal: ${getNthDecimalDigit(number, 3)}`);
console.log(`4th digit after decimal: ${getNthDecimalDigit(number, 4)}`);
console.log(`5th digit after decimal: ${getNthDecimalDigit(number, 5)}`);
console.log(`6th digit after decimal: ${getNthDecimalDigit(number, 6)}`);

// Additional examples
console.log(getNthDecimalDigit(98.7654, 2)); 
console.log(getNthDecimalDigit(-12.345, 3)); 
console.log(getNthDecimalDigit(1.2, 5));    



/*
run:

Number: 123.456789
1st digit after decimal: 4
2nd digit after decimal: 5
3rd digit after decimal: 6
4th digit after decimal: 7
5th digit after decimal: 8
6th digit after decimal: 9
6
5
0

*/

 



answered 1 day ago by avibootz
0 votes
/**
 * Returns the Nth digit after the decimal point.
 *
 * If we need maximum precision and want to avoid floating-point quirks, 
 * a string-based solution can be safer:
 */
function getNthDecimalDigitString(value, position) {
    if (!Number.isInteger(position) || position < 1) {
        throw new TypeError("position must be a positive integer.");
    }

    const decimalPart = String(Math.abs(value)).split(".")[1] || "";

    return Number(decimalPart[position - 1] || 0);
}

// -----------------------------------------------------------------------------
// Usage Examples
// -----------------------------------------------------------------------------

const number = 123.456789;

console.log(`Number: ${number}`);

console.log(`1st digit after decimal: ${getNthDecimalDigitString(number, 1)}`);
console.log(`2nd digit after decimal: ${getNthDecimalDigitString(number, 2)}`);
console.log(`3rd digit after decimal: ${getNthDecimalDigitString(number, 3)}`);
console.log(`4th digit after decimal: ${getNthDecimalDigitString(number, 4)}`);
console.log(`5th digit after decimal: ${getNthDecimalDigitString(number, 5)}`);
console.log(`6th digit after decimal: ${getNthDecimalDigitString(number, 6)}`);

// Additional examples
console.log(getNthDecimalDigitString(98.7654, 2));
console.log(getNthDecimalDigitString(-12.345, 3));
console.log(getNthDecimalDigitString(1.2, 5));



/*
run:

Number: 123.456789
1st digit after decimal: 4
2nd digit after decimal: 5
3rd digit after decimal: 6
4th digit after decimal: 7
5th digit after decimal: 8
6th digit after decimal: 9
6
5
0

*/

 



answered 1 day ago by avibootz

Related questions

...