Truncar Double, como faço?

Como faço para truncar um Double?
De preferência que o truncamento já saia arredondado.
Obrigado.

Acredito que a classe NumberFormat seja o que vc precisa, de uma olhadinha na documentação que tenho certeza que vai te ajudar.

sds

Logan

Bem, a classe Math tem algumas funções para isso:

Math.roud(double a)

Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

Math.floor(double a)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases:
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

Math.ceil(double a)

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
If the argument value is less than zero but greater than -1.0, then the result is negative zero.

Math.round(10.6) = 11
Math.round(10.4) = 10

Math.floor(9.4) = 9
Math.floor(9.9) = 9

Math.ceil(7.6) = 8
Math.ceil(7.1) = 8

Era isso que você queria???

T+

É mais ou menos isto, só que quero que ele arredonde com duas casas decimais. ex.:

9.586 = 9.59
1.534 = 1.53
9.999 = 10.00

levando em consideração o terceiro dígito decimal: se >=5, aredonda para cima, senão para baixo, como no exemplo.

Olá,

Se você quer que o resultado for um double:

    public static double truncate(double value) {
        return Math.round(value * 100) / 100d;
    }

Se você quer um String, acho que seria algo tipo:

    public static String truncate(double value) {
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(value);
    }

[]s,
Sami

Era bem isso que eu precisava, agradeço a todos a colaboração, e caso seja útil, segue um outra função parecida que achei na internet:

Esta função faz o aredondamento e retorna em String sempre do mesmo tamanho.

val: valor a aredondar;
n: número de casas decimais;
w: tamanho da string desejada;

    static final String ZEROES = "000000000000";
    static final String BLANKS = "            ";
    static String format( double val, int n, int w) {
        
        //rounding
        double incr = 0.5;
        for( int j=n; j>0; j--) incr /= 10;
        val += incr;
        
        String s = Double.toString(val);
        int n1 = s.indexOf('.');
        int n2 = s.length() - n1 - 1;
        
        if (n>n2)      s = s+ZEROES.substring(0, n-n2);
        else if (n2>n) s = s.substring(0,n1+n+1);
        
        if( w>0 & w>s.length() ) s = BLANKS.substring(0,w-s.length()) + s;
        else if ( w<0 & (-w)>s.length() ) {
            w=-w;
            s = s + BLANKS.substring(0,w-s.length()) ;
        }
        return s;
    }

Truncate não é round…

10.698 nao pode ser 10.70 e sim 10.69

nao confundam truncate com round.