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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:50:54+00:00 2026-06-09T01:50:54+00:00

I am trying to round down a double using Big decimal and then comparing

  • 0

I am trying to round down a double using Big decimal and then comparing to another number For example:

    // Parse the string to extract the double and then put it into Big
    // decimal
    BigDecimal bdAns = BigDecimal.valueOf(Double.parseDouble("3.1419"));
    System.out.println(bdAns);
    BigDecimal bdCorr = BigDecimal.valueOf(Double.parseDouble("3.142"));
    System.out.println(bdCorr);
    System.out.println(bdCorr.precision() + " , " + bdAns.precision());
    // Round down the value of the bdAns, so that the precision of bdAns and
    // bdCorr matches. This code doesnt seem to work.
    bdAns = BigDecimal.valueOf(bdAns).setScale(bdCorr.precision(),
            BigDecimal.ROUND_UNNECESSARY);
    System.out.println(bdAns);
    System.out.println(bdAns.compareTo(bdCorr));

The last println is printing -1.But they should be equal as 3.1419 round to 3 places after decimal should be 3.142. Can somebody please tell me what is wrong with the code?

  • 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-09T01:50:55+00:00Added an answer on June 9, 2026 at 1:50 am

    precision and scale are not the same thing, it appears you are confusing the two. scale is the number of digits to the right of the decimal point. precision is the total number of digits.

    Change all of your references to precision to scale and it should work (but you’ll have to pick a rounding mode other than UNNECESSARY).

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

Sidebar

Related Questions

I am trying to round down a number using PHP's round() function. Here is
I'm trying to round the number down despite the value after the decimal point.
I'm trying to round a number to it's first decimal place and, considering the
I'm trying to round a number to the next smallest power of another number.
I'm trying to round down a number to the nearest say 15, 20, 30.
I am trying to round a number in JavaScript to match the following Excel
I have been trying in vain to round down the current time to the
I'm trying to do a round-about way of having dependent drop-down boxes. I'm hoping
I trying to find a script which will round down a date to the
I am trying to round the values in my array to 2 decimal points.

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.