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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:28:21+00:00 2026-06-06T22:28:21+00:00

To round a number I use the following code: public static roundBd(BigDecimal bd){ BigDecimal

  • 0

To round a number I use the following code:

public static roundBd(BigDecimal bd){
  BigDecimal result1 = bd.setScale(0, RoundingMode.HALF_UP);
  return result1;
}
  • Input 1.50 –> Output 2
  • Input 1.499 –> Output 1

The first result is ok for me, but the second is not what I expected.
Even for 1.499 I’d like to have in output 2. (In details: first I’d like to round 1.499 to 1.50 then to 1.5 and finally to 2)

But….

BigDecimal bd = new BigDecimal("1.499");  // I'd like to round it to 2
BigDecimal result1 = bd.setScale(2, RoundingMode.HALF_UP); // result1 == 1.50
BigDecimal result2 = bd.setScale(1, RoundingMode.HALF_UP); // result2 == 1.5
BigDecimal result3 = bd.setScale(0, RoundingMode.HALF_UP); // result3 == 1
  • 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-06T22:28:24+00:00Added an answer on June 6, 2026 at 10:28 pm

    That’s not the way rounding works. HALF_UP means that if a number is exactly in the middle between the 2 closest available values (depending on the scale), it will be rounded up. Anything else is rounded to the closest value.

    Extract from the javadoc:

    Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant

    To get to the behaviour you require, you could round successively although I’m not sure why you want such a behaviour:

    BigDecimal bd = new BigDecimal("1.499");  // I'd like to round it to 2
    BigDecimal result1 = bd.setScale(2, RoundingMode.HALF_UP); // result1 == 1.50
    BigDecimal result2 = result1.setScale(0, RoundingMode.HALF_UP); // result2 == 2
    
    • 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 a number in JavaScript to match the following Excel
I use the following statement to create a percentage calculation in a view: round(((
The following python code calculates the number of iterations to do stuff based on
I want to make round a number to million. This is my code: string
Possible Duplicate: How to round a number to n decimal places in Java I
I'm trying to round a number to it's first decimal place and, considering the
I was curious to know how I can round a number to the nearest
I want to round a float number to a given precision, for example :
How can we round off a number to the nearest 10 in php? Say
I am trying to design the backend and have the following use case. I

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.