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

103
Views
La mejor manera de formatear un valor doble a 2 decimales

Estoy tratando con muchos valores dobles en mi aplicación, ¿hay alguna manera fácil de manejar el formato de valores decimales en Java?

¿Hay alguna otra manera mejor de hacerlo que

 DecimalFormat df = new DecimalFormat("#.##");

Lo que quiero hacer básicamente es formatear valores dobles como

 23.59004 to 23.59 35.7 to 35.70 3.0 to 3.00 9 to 9.00
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

No, no hay mejor manera.

En realidad tienes un error en tu patrón. Lo que quieres es:

 DecimalFormat df = new DecimalFormat("#.00");

Tenga en cuenta el "00" , que significa exactamente dos lugares decimales.

Si usa "#.##" ( # significa dígito "opcional"), eliminará los ceros finales, es decir, new DecimalFormat("#.##").format(3.0d); imprime solo "3" , no "3.00" .

over 4 years ago · Santiago Trujillo Report

0

Una alternativa es usar String.format :

 double[] arr = { 23.59004, 35.7, 3.0, 9 }; for ( double dub : arr ) { System.out.println( String.format( "%.2f", dub ) ); }

producción:

 23.59 35.70 3.00 9.00

También puede usar System.out.format (firma del mismo método) o crear un java.util.Formatter que funcione de la misma manera.

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!