Skip to content
The Chat Archive
claude-opus-4-20250514-thinking-16k· Anthropic LMArena Human Preference 140K

Remove leading zero from a float when printing in c++, eg. 12.34 only show .34

  1. User

    Remove leading zero from a float when printing in c++, eg. 12.34 only show .34

  2. Assistant

    Here are several ways to print only the decimal part of a float in C++:

    Method 1: Using Mathematical Operations

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    int main() {
        float num = 12.34;
        float fractional = num - std::floor(num);
        
        std::cout << fractional << std::endl;  // Output: 0.34
        return 0;
    }
    

    Method 2: String Manipulation

    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main() {
        float num = 12.34;
        
        // Convert to string
        std::ostringstream oss;
        oss << std::fixed << num;
        std::string str = oss.str();
        
        // Find decimal point and print from there
        size_t pos = str.find('.');
        if (pos != std::string::npos) {
            std::cout << str.substr(pos) << std::endl;  // Output: .34
        }
        
        return 0;
    }
    

    Method 3: Using modf() Function

    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    int main() {
        float num = 12.34;
        float intpart;
        float fractpart = std::modf(num, &intpart);
        
        // Print with custom formatting
        std::cout << std::fixed << std::setprecision(2);
        std::cout << fractpart << std::endl;  // Output: 0.34
        
        // To get exactly ".34" format:
        if (fractpart != 0) {
            std::cout << "." << int(fractpart * 100) << std::endl;  // Output: .34
        }
        
        return 0;
    }
    

    Method 4: Direct String Formatting

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    int main() {
        float num = 12.34;
        float fractional = num - std::floor(num);
        
        // Format to show only decimal part with dot
        if (fractional != 0) {
            std::cout << std::fixed << std::setprecision(2);
            std::cout << "." << int(fractional * 100) << std::endl;  // Output: .34
        }
        
        return 0;
    }
    

    Complete Example with Multiple Numbers

    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    void printFractionalPart(float num) {
        float fractional = num - std::floor(num);
        
        if (fractional == 0) {
            std::cout << ".0";
        } else {
            // Determine precision needed
            std::cout << std::fixed << std::setprecision(6);
            std::string temp = std::to_string(fractional);
            size_t pos = temp.find('.');
            if (pos != std::string::npos) {
                std::cout << temp.substr(pos);
            }
        }
    }
    
    int main() {
        float numbers[] = {12.34, 5.789, 100.5, 0.123, 42.0};
        
        for (float num : numbers) {
            std::cout << num << " -> ";
            printFractionalPart(num);
            std::cout << std::endl;
        }
        
        return 0;
    }
    

    The string manipulation approach (Method 2) gives you the most control over the exact format, while the mathematical approaches are more efficient for numerical operations. Choose based on whether you need the result as a number for further calculations or just for display purposes.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.