Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:58:45+00:00 2026-06-03T16:58:45+00:00

I need to convert double to string with given precision. String.format("%.3f", value) (or DecimalFormat

  • 0

I need to convert double to string with given precision. String.format("%.3f", value) (or DecimalFormat) does the job, but benchmarks show that it is slow. Even Double.toString conversion takes about 1-3 seconds to convert 1 million numbers on my machine.

Are there any better way to do it?

UPDATE: Benchmarking results

Random numbers from 0 to 1000000, results are in operations per millisecond (Java 1.7.0_45), higher is better:

Benchmark                                    Mean   Mean error    Units

String_format                             747.394       13.197   ops/ms
BigDecimal_toPlainString                 1349.552       31.144   ops/ms
DecimalFormat_format                     1890.917       28.886   ops/ms
Double_toString                          3341.941       85.453   ops/ms
DoubleFormatUtil_formatDouble            7760.968       87.630   ops/ms
SO_User_format                          14269.388      168.206   ops/ms

UPDATE:

Java 10, +ryu, higher is better:

                                Mode  Cnt      Score      Error   Units
String_format                  thrpt   20    998.741 ±   52.704  ops/ms
BigDecimal_toPlainString       thrpt   20   2079.965 ±  101.398  ops/ms
DecimalFormat_format           thrpt   20   2040.792 ±   48.378  ops/ms
Double_toString                thrpt   20   3575.301 ±  112.548  ops/ms
DoubleFormatUtil_formatDouble  thrpt   20   7206.281 ±  307.348  ops/ms
ruy_doubleToString             thrpt   20   9626.312 ±  285.778  ops/ms
SO_User_format                 thrpt   20  17143.901 ± 1307.685  ops/ms
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:58:48+00:00Added an answer on June 3, 2026 at 4:58 pm

    Disclaimer: I only recommend that you use this if speed is an absolute requirement.

    On my machine, the following can do 1 million conversions in about 130ms:

     private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
     
     public static String format(double val, int precision) {
         StringBuilder sb = new StringBuilder();
         if (val < 0) {
             sb.append('-');
             val = -val;
         }
         int exp = POW10[precision];
         long lval = (long)(val * exp + 0.5);
         sb.append(lval / exp).append('.');
         long fval = lval % exp;
         for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
             sb.append('0');
         }
         sb.append(fval);
         return sb.toString();
     }
    

    The code as presented has several shortcomings: it can only handle a limited range of doubles, and it doesn’t handle NaNs. The former can be addressed (but only partially) by extending the POW10 array. The latter can be explicitly handled in the code.


    If you don’t need thread-safe code, you can re-use the buffer for a little more speed (to avoid recreating a new object each time), such as:

      private static final int[] POW10 = {1, 10, 100, 1000, 10000, 100000, 1000000};
      private static final StringBuilder BUFFER = new StringBuilder();
    
      public String format( double value, final int precision ) {
        final var sb = BUFFER;
        sb.setLength( 0 );
    
        if( value < 0 ) {
          sb.append( '-' );
          value = -value;
        }
    
        final int exp = POW10[ precision ];
        final long lval = (long) (value * exp + 0.5);
    
        sb.append( lval / exp ).append( '.' );
    
        final long fval = lval % exp;
    
        for( int p = precision - 1; p > 0 && fval < POW10[ p ]; p-- ) {
          sb.append( '0' );
        }
    
        sb.append( fval );
    
        return sb.toString();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string value in that I need to convert to double in VB.Net.
I need to convert a HashMap<String, Object> to an array; could anyone show me
I need to convert minutes (defined as Integer) into the following String format hh:mm
How to convert double to a string value that I get from the spinner,
I have some customized string formats of double values. I need to convert these
I am developing an android app. I need to convert string to double. How
I need to convert double to string with two decimal digits separated with 'dot'
I have a need to convert NSString to double. If this string is in
I need to be able to convert from a Delphi Real48 to C# double.
I need to convert a string to another which has removed anything before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.