2

I would like to format some double values to a specific number of digits ignoring starting zeros.

Example, lets say format to 6 digits:

131.468627436358  ->  131.469
3.16227766016838  ->  3.16228
0.66018099039325  ->  0.660181
0.02236067977499  ->  0.0223607
1

4 Answers 4

3

BigDecimal allows handling of significant figures correctly. This:

MathContext round3SigFig = new MathContext(3,RoundingMode.HALF_UP);
System.out.println((new BigDecimal(0.000923874932)).round(round3SigFig));

produces:

0.000924

Obviously, though, passing your floating points through an arbitrary precision object representation isn't ideal.

0

Consider this as the last chance option: How about converting the number to string, taking first six digits with having the "," in mind and converting back to double.

0

I believe this is closely related to the following question: Format double values using a maximum of five total digits, rounding decimal digits if necessary

There's an answer in the question I linked to which uses MathContext and BigDecimal (like maybeWeCouldStealAVan's answer). However, that didn't quite work for me because I cared about the total number of digits. It may work for you, however.

I ended up writing my own custom solution which formatted exactly as I needed. Perhaps this also meets your requirements, or can be easily modified to meet them:

public static String format( double value, int totalDigits )
{
    String s = String.valueOf( value );
    int decimal = s.indexOf( '.' );

    // there is no decimal part, so simply return the String
    if ( decimal == -1 )
    {
        return s;
    }
    else
    {
        int finalLength;

        // example: 23.34324
        // the final result will be length totalDigits + 1 because we will include the decimal
        if ( decimal < totalDigits )
        {
            finalLength = totalDigits + 1;
        }
        // example: 99999
        // the final result will be length totalDigits because there will be no decimal
        else if ( decimal == totalDigits )
        {
            finalLength = totalDigits;
        }
        // example: 999999.999
        // we can't make the final length totalDigits because the integer portion is too large
        else
        {
            finalLength = decimal;
        }

        finalLength = Math.min( s.length( ), finalLength );

        return s.substring( 0, finalLength );
    }
}

public static void main( String[] args )
{
    double[] data = { 1, 100, 1000, 10000, 100000, 99999, 99999.99, 9999.99, 999.99, 23.34324, 0.111111 };
    for ( double d : data )
    {
        System.out.printf( "Input: %10s \tOutput: %10s\n", Double.toString( d ), format( d, 5 ) );
    }
}
0

Use logarithmic Functions to calculate the number of additional digits you need.

public static int leadingZeros (double d) {
    return (d >= 1.0) ? 0 : (int) (-1 * (Math.floor (Math.log (d) / Math.log (10))));
}

For

    System.out.println (leadingZeros (4));
    System.out.println (leadingZeros (0.4));
    System.out.println (leadingZeros (0.04));
    System.out.println (leadingZeros (0.004));

it return 0, 1, 2, 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.