Java格式加倍到忽略零的特定位数

时间:2012-04-22 20:01:35

标签: java format double

我想将一些双值格式化为忽略起始零的特定位数。

示例,假设格式为6位数:

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

4 个答案:

答案 0 :(得分:3)

BigDecimal允许正确处理重要数字。这样:

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

产生

0.000924

显然,通过任意精度对象表示传递浮点并不理想。

答案 1 :(得分:0)

将此视为最后的机会选项:如何将数字转换为字符串,将前六位数设为“,”并将其转换为双数。

答案 2 :(得分:0)

我认为这与以下问题密切相关:Format double values using a maximum of five total digits, rounding decimal digits if necessary

在我链接的问题中有一个答案,使用MathContextBigDecimal(就像MaybeWeCouldStealAVan的答案)。但是,这对我来说并不适用,因为我关心的是总数。但是,它可能适合你。

我最终编写了自己的自定义解决方案,其格式完全符合我的要求。也许这也符合您的要求,或者可以轻松修改以满足它们:

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 ) );
    }
}

答案 3 :(得分:0)

使用对数函数计算所需的附加位数。

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

有关

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

它返回0,1,2,3。