Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

616
Views
Kotlin. ¿Cómo formatear un número decimal con cero al final?

Necesito formatear el número.

Necesito el siguiente resultado:

 3434 -> 3 434 3434.34 -> 3 434.34 3434.3 -> 3 434.30

Aquí está mi código:

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

Pero obtengo un resultado como este:

 3434 -> 3 434 3434.34 -> 3 434.34 3434.3 -> 3 434.3 // wrong!! expected 3 434.30

Necesito agregar cero al final si hay un dígito después del punto decimal.

Por favor, ayúdenme, ¿cómo puedo solucionar el problema?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Ejemplo:

 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); } }

Simplificado

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

0

Eliminar los dos últimos # después de . y agregue 00 en lugar de ## .

 val dec = DecimalFormat("#,###,##0.00")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!