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

  • Home
  • SEARCH
  • 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 8599177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:20:14+00:00 2026-06-12T01:20:14+00:00

I have the following function that is used to round a double value in

  • 0

I have the following function that is used to round a double value in Java:

public static double round(double d, int decimalPlace) {

    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

As input, this function is receiving these values:

double d = 7.3149999999999995;
int decimalPlace = 2

But, when the function returns, the value returned is 7.31, instead of 7.32.
I searched on the docs to see why the bd.SetScale is with that behavior, but with no success.

Does anybody could me explain why is this happening?
Thanks a lot!!

  • 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-12T01:20:16+00:00Added an answer on June 12, 2026 at 1:20 am

    Does anybody could me explain why is this happening?

    It’s obeying the documented behaviour 🙂 From the docs:

    If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division.

    And for RoundingMode.HALF_UP:

    Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.

    Now 7.3149999999999995 isn’t equidistant to both 7.31 and 7.32 – it’s closer to 7.31, so that’s the result.

    The difference between HALF_UP and HALF_DOWN would only be seen if the original value were exactly 7.315, i.e. half way between the two.

    As an aside, to make sure you get exactly the number you’d expect to start with, I’d suggest using a String. For example:

    double d = 0.1;
    BigDecimal bd = new BigDecimal(d);
    

    isn’t the same as:

    BigDecimal bd = new BigDecimal("0.1");
    

    Converting from double to BigDecimal is usually a sign that you’ve got something wrong, and you should be using one type consistently throughout.

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

Sidebar

Related Questions

I have the following function that is used as the glutKeyboardFunc function parameter: void
I have the following function that is giving me variable declared and not used
I have a gridview, the datasource of which is the following function: public static
I'm working with python and import dll that have the following function: int ReadMem(long
I have the following recursive function that is used to search down a hierarchical
I have the following function that works properly in Chrome and Firefox, but not
I have the following function that does string splitting: on splitText(aString, delimiter) set retVal
I have the following function that deletes the LaTeX command surrounding the current cursor
I have the following function that I am using to remove the characters \04
I have the following function that takes a number like 5 and creates a

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.