Categorías
Otros

Cómo mantener dos puntos decimales en Java

// BigDecimal.Round_Half_UP Represents rounded, setscale (2) indicates that the two decimals are retained. ;
BigDecimal bd = new BigDecimal(a);
		Double tem = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//		Method Two
// Take the number to 100 after 100, then divide by 100.0
// java math.Round () is rounded from all, and does not set a few decimals.
// 3 is the radius because two digits are retained, so the value of 100 after 100 is rounded, and finally in addition to 100, because the value of the round, the value is int,
// 100 constant is also int, so the final result is int, to get a decimal, first turn the previous one into a Double type
System.out.println((double)Math.round(Math.PI * 3 * 3 * 100) / 100);

.

  C++ aprendiendo cuatro

Por Programación.Click

Más de 20 años programando en diferentes lenguajes de programación. Apasionado del code clean y el terminar lo que se empieza. ¿Programamos de verdad?

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *