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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:40:27+00:00 2026-06-10T15:40:27+00:00

I started using Sonar recently in a project, and i got a PMD rule

  • 0

I started using Sonar recently in a project, and i got a PMD rule broken about using the constructor new BigDecimal(double val). When i read the java documentation, i found that new BigDecimal(double val) is somewhat unpredictable and that I should use new BigDecimal(String val) which is predictable.

Here is what javadoc says for BigDecimal public BigDecimal(double val):

Translates a double into a BigDecimal which is the exact decimal
representation of the double’s binary floating-point value. The scale
of the returned BigDecimal is the smallest value such that (10scale ×
val) is an integer.

Notes:

The results of this constructor can be somewhat unpredictable. 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. This is because 0.1 cannot be represented exactly as a double (or, for that
matter, as a binary fraction of any finite length). Thus, the value
that is being passed in to the constructor is not exactly equal to
0.1, appearances notwithstanding.

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.

When a double must be used as a source for a BigDecimal, note that
this constructor provides an exact conversion; it does not give the
same result as converting the double to a String using the
Double.toString(double) method and then using the
BigDecimal(String) constructor. To get that result, use the static
valueOf(double) method.

Why does this constructor really exists? Isnt new BigDecimal(String val) enough for that matter? When should I use the new BigDecimal(double val) constructor?

  • 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-10T15:40:29+00:00Added an answer on June 10, 2026 at 3:40 pm

    Why does this constructor really exists?

    It converts the actual represented value of double to a BigDecimal. The whole point of BigDecimal is to give as much precision as possible and that is what this constructor does.

    If you want to take the value you would get with a small amount of rounding the Double.toString(double) uses you can use

    System.out.println(BigDecimal.valueOf(0.1));
    

    prints

    0.1
    

    When should I use the new BigDecimal(double val) constructor

    When you want to know the value double really represents. You can apply your own rounding as required.

    When you use double you should always apply a sensible rounding. But, if you did that you may find you don’t need BigDecimal. 😉

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

Sidebar

Related Questions

Started using Quartz.net in my project. But got stuck. Using cron trigger fires only
I started using Thrift about a week ago. I got the latest source from
I started using ActiveAdmin recently in a project and almost everything works great but
I started using GitHub this weekend for a new personal project (we also use
I started using backbone for my projects. I finished a simple project and it
just started using log4j in one of my home-projects and I was just about
I am new user of Sonar. I have a small project in java which
Started using omnifocus recently and really like it. I prefer gmail's web interface, so
I started using the new Authorization system for apps. I seem to be experiencing
I started using Hibernate only recently. While I was playing with it, I experienced

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.