-1

I'm a newbie to Java and have to implement a solution at work to calculate cumulative difference. I am extracting data from a flat file using Informatica powercenter. One of the columns is total deductions for a department. The logic required to transform data in this column is below.

If Total deductions<=9999999.99, then display the value as is, ie 9999999.99

If Total deductions>9999999.99, then display 9999999.99 and in the next line display the difference between 9999999.99 and incoming value. For ex, if incoming value is 10000000.99, then display 9999999.99 1

If total deductions = 20000000.98 then display the below 9999999.99 9999999.99 1

I have the below code where I am hard coding values, and feel like this can be accomplished dynamically.

package day1.examples;

public class MedicalCenter {

    public static void main(String[] args) {

        double v=9999999.99;
        double i=20000000.98;

        if (i<v) {  
            System.out.println(i);          
        }
        if (i>v && i<=9999999.99*2) { 
            System.out.println(9999999.99);
            System.out.println(i-v);
        }
        if (i>v && i<=9999999.99*3) {
            System.out.println(9999999.99); 
            System.out.println(9999999.99);
            System.out.println(i-9999999.99*2);
        };
    }
}

Sample Output:

9999999.99
9999999.99
1.0

zx485
  • 28,498
  • 28
  • 50
  • 59
kabdul
  • 29
  • 5
  • Have you considered a loop? – Joe C Jan 21 '19 at 22:23
  • It would help a lot if you showed sample input and expected output. – Bohemian Jan 21 '19 at 22:24
  • I did. I divided the incoming value with the fixed values (9999999.99). If the quotient was <=1, then I display that value. If its greater than 1 then I would like to pass that value through a loop, which I was unable to do so. – kabdul Jan 21 '19 at 22:25
  • I have the sample code and result published on this screen – kabdul Jan 21 '19 at 22:27

1 Answers1

-1

You need to either keep subtracting by 9,999,999.99 until you get the remainder, or you can use division. I'll show you an example of subtraction.

public static void main(String[] args) {                                        
    double input; //need to implement this                                      
    double v=9999999.99;                                                        

    while(input > v) {                                                          
        System.out.print(v + " ");                                              
        input -= v; //subtract                                                  
    }                                                                           
    System.out.println(input);                                                  
}  

The idea is that you keep subtracting v from the input number until you can't subtract anymore, and each time you subtract you also print out v together with a space. After you're done subtracting, you simply print the remainder of the input.

As far as I know, division is just a glorified form of subtraction in the end, so this is possibly simpler than using division—not only by producing cleaner code (which is often the highest priority), but perhaps also performance wise.

You will have floating point arithmetic issues due to the subtraction however, so you should use BigDecimal instead of double. More information on that here: Is floating point math broken?

RaminS
  • 2,208
  • 4
  • 22
  • 30
  • Hi, Yes.. I have an Input value (i=20000000.98) used for testing in my sample code. I chose this value as it is (2 * 9999999.99) , the o/p I want and get is 9999999.99 , 9999999.99, 1. – kabdul Jan 21 '19 at 23:54
  • @kabdul I know. Is my answer not sufficient? Try with `double input = 20000000.98;` in my code and see what happens. – RaminS Jan 21 '19 at 23:56
  • 1
    I tried your solution, but I always get 9999999.99, 9999999.99, 1 as the o/p. My requirement is, If input value is <= 9999999.99, then I should display the Input value as is. If its greater than 9999999.99, then I should display 9999999.99 and the difference. Ex, if my input is 10000000.99, the code should return 999999.99, 1. If my input is 40000000.96, then my code should return 9999999.99, 9999999.99, 9999999.99, 9999999.99, 1 – kabdul Jan 22 '19 at 00:13
  • @kabdul No. It works as you are describing; you can see it [here](https://rextester.com/AUXM22152). However, there is another issue due to floating point arithmetics that you need to address. You can find more info on that here: https://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-results – RaminS Jan 22 '19 at 00:24
  • For the floating point issue, I am using System.out.printf("%.2f",input); This loads only 2 decimal points. – kabdul Jan 22 '19 at 05:25