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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:07:55+00:00 2026-05-26T18:07:55+00:00

The source for round in apache commons looks like this: public static double round(double

  • 0

The source for round in apache commons looks like this:

public static double round(double x, int scale, int roundingMethod) {
    try {
        return (new java.math.BigDecimal(Double.toString(x)).setScale(scale, roundingMethod)).doubleValue();
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(x)) {
            return x;
        } else {
            return Double.NaN;
        }
    }
}

I was wondering, when creating the BigDecimal why did they chose to convert the double to a string (using the Double.toString) instead of simply using the double itself?

In other words, what’s wrong with this? :

public static double round(double x, int scale, int roundingMethod) {
    try {
        return (new java.math.BigDecimal(x).setScale(scale, roundingMethod)).doubleValue();
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(x)) {
            return x;
        } else {
            return Double.NaN;
        }
    }
}
  • 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-05-26T18:07:56+00:00Added an answer on May 26, 2026 at 6:07 pm

    It’s because the result of BigDecimal(double) constructor is unpredictable as mentioned in javadoc.

    One might assume that writing new BigDecimal(0.1) in Java creates a
    BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with
    a scale of 1), but it is actually equal to
    0.1000000000000000055511151231257827021181583404541015625

    The String constructor, on the other hand, is perfectly predictable:
    writing new BigDecimal(“0.1”) creates a BigDecimal which is exactly
    equal to 0.1, as one would expect. Therefore, it is generally
    recommended that the String constructor be used in preference to this
    one.

    Test Case:

    System.out.println(java.math.BigDecimal.valueOf(0.1).toString());
    System.out.println(new java.math.BigDecimal(0.1).toString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Source: Facebook Hacker Cup Qualification Round 2011 A double-square number is an integer X
I'd like to programmatically create diagrams like this (source: yaroslavvb.com ) I imagine I
I want to round specific corners of buttons to create something like mx.controls.ButtonBar. But
If you look at the source of this page http://kingston.talking-newspapers.co.uk/ you will see a
Designed specifically for input buttons such as <input type=submit value=Button Name> , this round
I've come round to the idea that Microsoft's SAL (Source Annotation Language) is a
I can't get my head round this one. I've tried to adhere to the
I have been looking round for an open-source CMS framework that I can use
I am getting this message Check that your C# source compiles and that you
I came across this regular expression in the jQuery source code: ... rmozilla =

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.