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 7747581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:33:58+00:00 2026-06-01T10:33:58+00:00

The SO community was right, profiling your code before you ask performance questions seems

  • 0

The SO community was right, profiling your code before you ask performance questions seems to make more sense then my approach of randomly guessing 🙂 I profiled my code(very intensive math) and didn’t realize over 70% of my code is apparently in a part I didn’t think was a source of slowdown, rounding of decimals.

static double roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.###");
    return Double.valueOf(twoDForm.format(d));
}

My problem is I get decimal numbers that are normally .01,.02,etc..but sometimes I get something like .070000000001 (I really only care about the 0.07 but floating point precision causes my other formulas that result to fail), I simply want the first 3 decimals to avoid this problem.

So is there a better/faster way to do this?

  • 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-01T10:33:59+00:00Added an answer on June 1, 2026 at 10:33 am

    The standard way to round (positive) numbers would be something like this:

    double rounded = floor(1000 * doubleVal + 0.5) / 1000;
    

    Example 1: floor(1000 * .1234 + 0.5) / 1000 = floor(123.9)/1000 = 0.123
    Example 2: floor(1000 * .5678 + 0.5) / 1000 = floor(568.3)/1000 = 0.568

    But as @nuakh commented, you’ll always be plagued by rounding errors to some extent. If you want exactly 3 decimal places, your best bet is to convert to thousandths (that is, multiply everything by 1000) and use an integral data type (int, long, etc.)

    In that case, you’d skip the final division by 1000 and use the integral values 123 and 568 for your calculations. If you want the results in the form of percentages, you’d divide by 10 for display:

    123 → 12.3%
    568 → 56.8%

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have five divs on my page (#art #fashion, #self, #nightlife, #community). right now,
It seems as though the general consensus of the testing community is to not
I'm not sure the right way to do this, so I wanted to ask
I've got a social community with a lot of traffic. In the right column
Hello there community :), So I'm a real java newb, right? I can't seem
Community Wiki Question Every time I work on a project involving passwords or securing
Community wiki'ed already, folks. What part of Apache Commons saves you the most time?
[community edit: original title was python conditionals, OP is asking what is wrong with
Dear community members, I am actually a bit new to CSS, however I know
Dear community members, I have a small problem, which involves a text input field

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.