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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:11:37+00:00 2026-05-26T15:11:37+00:00

I have some constants f.e.: BigDecimal ceiling1 = new BigDecimal(5); BigDecimal ceiling2 = new

  • 0

I have some constants f.e.:

BigDecimal ceiling1 = new BigDecimal(5);
BigDecimal ceiling2 = new BigDecimal(10);
BigDecimal ceiling3 = new BigDecimal(20);

BigDecimal rate1 = new BigDecimal(0.01);
BigDecimal rate2 = new BigDecimal(0.02);
BigDecimal rate3 = new BigDecimal(0.04);
BigDecimal rate4 = new BigDecimal(0.09);

Now based on a parameter f.e.:

BigDecimal arg = new BigDecimal(6);

I want to retrieve the right rate which is based on this if structure (simplified):

if(arg <= ceiling1) {
   rate = rate1;
}else if(arg <= ceiling2) {
   rate = rate2;
} else if (arg <= ceiling3) {
   rate = rate3;
}else rate = rate4;

So in my example rate should be rate2

But I’m wondering if someone knows a better way to implement this, instead of a bunch of ifs.

Any pointers are welcome!

PS: I know my code isn’t 100% right, just wanted to show the idea

  • 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-26T15:11:38+00:00Added an answer on May 26, 2026 at 3:11 pm

    You can store your ceilings as keys in a TreeMap and your rates as values. Then use floorEntry and see also here.

    final TreeMap<BigDecimal, BigDecimal> rates = new TreeMap<BigDecimal, BigDecimal>();
    rates.put(new BigDecimal(0), new BigDecimal(0.01));
    rates.put(new BigDecimal(5), new BigDecimal(0.02));
    rates.put(new BigDecimal(10), new BigDecimal(0.04));
    rates.put(new BigDecimal(20), new BigDecimal(0.09));
    
    System.out.println(rates.floorEntry(new BigDecimal(0)).getValue());
    System.out.println(rates.floorEntry(new BigDecimal(6)).getValue());
    System.out.println(rates.floorEntry(new BigDecimal(10)).getValue());
    System.out.println(rates.floorEntry(new BigDecimal(100)).getValue());
    

    Test: http://ideone.com/VrucK. You may want to use a different representation as you can see in the test it looks ugly(Like Integers for the ceiling). Btw the ugly output comes from the fact that 0.01 is a double which does funny things with decimal representations.

    Edit: Suggested cleanup.

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

Sidebar

Related Questions

I have some constants that represent the valid options in one of my model's
Possible Duplicate: Avoiding repeated constants in CSS We have some theme colors that are
I have a lot of constants that are somehow related, at some point I
I have some std::list<char> list_type Now I have to supply contents of the list
As per subject. I have some constants hash defined like so: #define CONST 40
I have defined some constants eg: define('DB_HOSTNAME', 'localhost', true); define('DB_USERNAME', 'root', true); define('DB_PASSWORD', 'root',
I have added some constants to a model say MyModel.rb as shown below. MY_CONST
I have a class in VB with some constants that represent the string name
I have a Perl module that I have declared some constants: use constant BASE_PATH
I have a class in which I want to define some constants used by

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.