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

253
Vistas
How to express "1.275" to "1.28" with Decimal Format in Java
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
decimalFormat.applyPattern(".00");

System.out.print(decimalFormat.format(63.275));
// output : 63.27

System.out.print(decimalFormat.format(64.275));
// output : 64.28

Why are they different?

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

0

The value of 63.275 is recorded in computer as 63.27499999999999857891452847979962825775146484375 . As per the Java API doc "https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html#HALF_UP " Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for RoundingMode.DOWN. so ,

63.27499999999999857891452847979962825775146484375 ~ 63.27
64.275000000000005684341886080801486968994140625 ~ 64.28
over 4 years ago · Santiago Trujillo Denunciar

0

public static double roundByPlace(double number, int scale){
    BigDecimal bigDecimal = BigDecimal.valueOf(number);
    String pattern = "0.";
    for(int i = 0; i<scale; i++){
        pattern = pattern+"0";
    }
    DecimalFormat decimalFormat = new DecimalFormat(pattern);
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    double result = Double.parseDouble(decimalFormat.format(bigDecimal));
    return result;
}
over 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