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

626
Vistas
Kotlin. How to format decimal number with zero at the end?

I need to format the number.

I need the following result:

3434 -> 3 434

3434.34 -> 3 434.34

3434.3 -> 3 434.30

Here is my code:

  val formatter = DecimalFormat("#,###,##0.##")
            return formatter.format(value)

But I get a result like this:

3434 -> 3 434

3434.34 -> 3 434.34

3434.3 -> 3 434.3 // wrong!! expected 3 434.30

I need to add zero at the end if there is one digit after the decimal point.

Please help me how I can fix the problem?

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

0

Example:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;  

public class AddZeroToOneDigitDecimal{

     public static void main(String []args){
       System.out.println(customFormat(3434));
       System.out.println("----------");
       System.out.println(customFormat(3434.34));
       System.out.println("----------");
       System.out.println(customFormat(3434.3));
     }

     public static String customFormat(double d){

        String result =  formatter.format(d);
        return (result.replace(".00",""));
     }
     
     private static DecimalFormat formatter;
     private static final String DECIMAL_FORMAT = "#,###,##0.00";
     private static DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    
     static {
         formatSymbols.setDecimalSeparator('.');
         formatSymbols.setGroupingSeparator(' ');
         formatter = new DecimalFormat(DECIMAL_FORMAT, formatSymbols);
     }
}

Simplified

 val formatter = DecimalFormat("#,###,##0.00");
 return formatter.format(value).replace(".00","");
over 4 years ago · Santiago Trujillo Denunciar

0

Remove last two # after . and add 00 in place of ##.

val dec = DecimalFormat("#,###,##0.00") 
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