How to express a decimal number as a fixed-length string with leading zeros in Java

2 Answers

0 votes
public class FormatDecimalWithZeros {

    /**
        format_decimal_with_zeros
        -------------------------
        Converts a floating‑point number into a fixed‑length string with
        leading zeros on the integer part.

        Parameters:
            num          - the decimal number to format
            width        - total width of the integer part (zero‑padded)
            decimals     - number of digits after the decimal point

        Returns:
            A formatted string such as "00003.14159"

        Example:
            num = 3.14159, width = 5, decimals = 5
            Output → "00003.14159"
    */
    public static String formatDecimalWithZeros(double num, int width, int decimals) {

        // Split into integer and fractional parts
        int integer_part = (int) num;
        double fractional_part = num - integer_part;

        // Format integer part with leading zeros
        String int_str = String.format("%0" + width + "d", integer_part);

        // Format fractional part (starts with "0.xxx")
        String frac_str = String.format("%." + decimals + "f", fractional_part);

        // Remove the leading "0" before the decimal point
        return int_str + frac_str.substring(1);
    }

    public static void main(String[] args) {
        double num = 3.14159;

        String result = formatDecimalWithZeros(num, 5, 5);

        System.out.println("Original number: " + String.format("%.5f", num));
        System.out.println("Formatted string: " + result);
    }
}



/*
run:
            
Original number: 3.14159
Formatted string: 00003.14159
        
*/


 



answered 8 hours ago by avibootz
0 votes
public class FormatDecimalWithZeros {

    /**
        format_decimal_with_zeros
        -------------------------
        Converts a floating‑point number into a fixed‑length string with
        leading zeros on the integer part.

        Parameters:
            num   - the decimal number to format
            len   - total width of the integer part (zero‑padded)

        Returns:
            A formatted string such as "00003.14159"

        Example:
            num = 3.14159, len = 5
            Output → "00003.14159"
    */

    public static String FormatDecimalWithZeros(double num, int len) {
        // Convert number to string and split into integer and decimal parts
        String[] parts = String.valueOf(num).split("\\.");

        String intPart = parts[0];
        String decPart = parts.length > 1 ? parts[1] : null;

        // Pad the integer part with leading zeros
        String padded = String.format("%" + len + "s", intPart).replace(' ', '0');

        // Reattach decimal part if it exists
        return (decPart != null) ? padded + "." + decPart : padded;
    }

    public static void main(String[] args) {
        double num = 3.14159;

        String result = FormatDecimalWithZeros(num, 5);

        System.out.println("Original number: " + num);
        System.out.println("Formatted string: " + result);
    }
}



/*
run:
            
Original number: 3.14159
Formatted string: 00003.14159
        
*/


 



answered 8 hours ago by avibootz
edited 7 hours ago by avibootz

Related questions

...