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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:23:38+00:00 2026-05-26T16:23:38+00:00

I need to do some calculations like this (simplified): amount1 amount2 amount3 amount4 total

  • 0

I need to do some calculations like this (simplified):

            amount1     amount2     amount3     amount4     total
            5000        500         1000        350         6850

3.55%       177.5       17.75       35.5        12.43       243.18
1.00%       50          5           10          3.5         68.5

-           4772.5      477.25      954.5       334.07      6538.32

16.66%      ....
...

The calculations are pretty easy; But I’m not sure how I best implement this.
I don’t only need to be able to fetch and show the total results from the calculations but also every sub calculation.
I can make a method for every sub calculation and group methods together in one for a total calculation.
f.e.:

calcTotal1() {
    calcSub1() + calcSub2() + ... ;
}

But I was wondering if there is a better way to implement this.
It also should be a little library that can be used by different programs.

  • 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-26T16:23:38+00:00Added an answer on May 26, 2026 at 4:23 pm

    I think, your requirement could be nicely met with an array of percentages to be calculated:

     public BigDecimal[] calculate(BigDecimal[] percentages, BigDecimal amount) {
         BigDecimal rolling = amount;
         BigDecimal[] result = new BigDecimal[percentages.length];
         for (int i = 0; i < percentages.length; i++) {
            // We use null as a marker for subtotals
            if (percentages[i] != null) {
               result[i] = amount.multiply(percentages[i]).divide(new BigDecimal("100"), MathContext.DECIMAL128);
               rolling = rolling.add(result[i]);
            } else {
               // Insert the subTotal
               result[i] = rolling;
            }
         }
    

    Please note the use of BigDecimals, while they do use a bit more memory, it will have a severe impact on the correctness of your figures.

    With nulls in the percentages[], you can specify rows where subtotals have to be printed out.

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

Sidebar

Related Questions

I have an application in Java/JSF, I need to do some optimization calculations, like
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Okay, so I need something like this: time_span = 1.month date = DateTime.now date
I'm not that smart like some of you geniuses. I need some help from
I have 2:01:20 PM format in one label; after some calculations, I need to
My calculations are something like this: f[x_]:= (*runs fast*) g[x_]:=(*runs fast*) h[x_]=depends on f[x],g[x]
When a user visits an .aspx page, I need to start some background calculations
I have some useful methods for physics calculations like acceleration / deceleration, speed, and

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.