Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
BigDecimal divide with scale

I have

private static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
    private static final BigDecimal TEN = new BigDecimal(10);

BigDecimal decimal = new BigDecimal(1050); I need to get 10% I write BigDecimal decimalResult = decimal.divide(ONE_HUNDRED).multiply(TEN)//100, 10

But Intellij IDE says:

'BigDecimal.divide()' called without a rounding mode argument more...

I added BigDecimal.ROUND_HALF_UP and all others but I get wrong result. I need 1050/100 = 10.5 but if I add BigDecimal.ROUND_HALF_UP result = 11.

How can I correctly divide with scale parameters?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

public static double divide(double d1, double d2) {
    BigDecimal b1 = new BigDecimal(d1);
    BigDecimal b2 = new BigDecimal(d2);
    return b1.divide(b2, 2, RoundingMode.DOWN).doubleValue();
}
about 4 years ago · Santiago Trujillo Denunciar

0

This example returns 105.0000

public class TestBigDecimal {
  static BigDecimal decimal = new BigDecimal(1050).setScale(4, BigDecimal.ROUND_HALF_UP);
  static BigDecimal ONE_HUNDRED = new BigDecimal(100);
  static BigDecimal TEN = new BigDecimal(10);

  public static void main(String[] args)
  {
    BigDecimal decimalResult = decimal.divide(ONE_HUNDRED).multiply(TEN) ;
    System.out.println(decimalResult);
  }
}

You should adjust the scale during the creation of decimal variable.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda