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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to extract the first digit from a floating-point number in C++

3 Answers

0 votes
#include <iostream>
#include <cmath>    // For fabs, floor

// Function to extract the first digit of a floating-point number
int getFirstDigit(double num) {
    // Handle zero explicitly
    if (num == 0.0) {
        return 0;
    }

    // Work with absolute value to ignore sign
    num = std::fabs(num);

    // Shift decimal point until number >= 1
    while (num < 1.0) { // Run when num < 1.0
        num *= 10.0;
    }

    // Shift until number < 10
    while (num >= 10.0) { // Run when num > 10.0
        num /= 10.0;
    }

    // Floor to get the integer digit
    return static_cast<int>(std::floor(num));
}

int main() {
    double value = 48724.928;

    int firstDigit = getFirstDigit(value);
    std::cout << "First digit: " << firstDigit << "\n";
    
    firstDigit = getFirstDigit(0.9761);
    std::cout << "First digit: " << firstDigit << "\n";
}


/*
run:

First digit: 4
First digit: 9

*/

 



answered Nov 16, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <cmath>
 
// Function to extract the first digit of a floating-point number
int getFirstDigit(double num) {
    num = std::abs(num); // handle negative numbers
    std::string str = std::to_string(num);
     
    // Find first digit character
    for (char ch : str) {
        if (std::isdigit(ch)) {
            return ch - '0';
        }
    }
     
    return -1; // error case
}
 
int main() {
    double value = 48724.928;
 
    int firstDigit = getFirstDigit(value);
    std::cout << "First digit: " << firstDigit << "\n";
     
    firstDigit = getFirstDigit(0.9761);
    std::cout << "First digit: " << firstDigit << "\n";
}
 
 
/*
run:
 
First digit: 4
First digit: 0
 
*/

 



answered Nov 17, 2025 by avibootz
edited Nov 17, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <cmath>

// Function to extract the first digit of a floating-point number
int getFirstDigit(double num) {
    if (num == 0.0) return 0;
    
    num = std::abs(num);
    
    // Find number of digits before decimal point
    int exponent = static_cast<int>(std::floor(std::log10(num)));
    
    // Scale to get first digit
    double normalized = num / std::pow(10.0, exponent);
    
    return static_cast<int>(normalized);
}

int main() {
    double value = 48724.928;

    int firstDigit = getFirstDigit(value);
    std::cout << "First digit: " << firstDigit << "\n";
    
    firstDigit = getFirstDigit(0.9761);
    std::cout << "First digit: " << firstDigit << "\n";
}


/*
run:

First digit: 4
First digit: 9

*/

 



answered Nov 17, 2025 by avibootz
...