| ||||
Hola: Si sólo es para presentación en pantalla, puedes usar la clase DecimalFormat. La creas pasándole un patrón estilo DecimalFormat formateador = new DecimalFormat ("#####.##"); // hasta cinco cifras enteras y dos decimales y luego escribes así System.out.println (formateador.format (elNumero)); Se bueno. |
| ||||
Otra idea seria poder precisar el nº de decimales que deseas por ejemplo :
Código:
te deberia devolver 1,012 import java.lang.Math; ... //... double x=1,0123456789; System.out.println(redondear(x,3)); public static double redondear(double num,int ndecimal) { double aux0 = Math.pow(10,ndecimal); double aux = num * aux0; int tmp = (int) aux; return (double) (tmp / aux0) ; } |
| |||
http://java.sun.com/j2se/1.4.2/docs/...igDecimal.html Cita: The BigDecimal class gives its user complete control over rounding behavior, forcing the user to explicitly specify a rounding behavior for operations capable of discarding precision (divide(BigDecimal, int), divide(BigDecimal, int, int), and setScale(int, int)). Eight rounding modes are provided for this purpose. Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations (setScale) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale is the specified value |