Search code examples
javaif-statementmathradixoperation

Splitting units base of a number and separating remainders


I am trying to split a number of a base then separating the two numbers to get different outputs. (Keep in mind I just edited, my answer is the solution). This is left here so people that have a similar problem can find a solution. Thank you all!

So this is the idea:

If number >= 10 && of base 10

Then give me discounted price on 10 units

if number <= 0 && not base 10

Then add the discount for the number which has 10 units in it and the remainder without the discount (let's say 100% for simplicity sake of the numbers)

So to make a practical example If I order 25 units of x (at $1 each) and 15 units (at $1 each) of y the price will be:

x 20 units = $0 x 5 units = $5 total

y 10 units = $0 y 5 units = $5 total

This is a bit tricky and this is what I got so far:

double discountedmNI = (mNI - ((mNI/100)*10)) * mNIC;
double discountedmNIP = mNI - ((mNI/100)*10);

if(mNIC >= 10 && mNIC % 10 == 0){

    System.out.println("mNI " + discountedmNIP + " " + mNIC);
    System.out.println(discountedmNI);

}

else if (!mNIC % 10 == 0){

    System.out.println("mNI " + mNI + mNIC);
    System.out.println(mNI * mNIC);

}

I don't think I am defining separate the 10 units right

Thank you all!


Solution

  • EUREKA!

        double discountedmNIP = mNI - ((mNI/100)*10);
        int mNIC2 = (mNIC % 10);
        double mNIC2disc = (mNI * mNIC2);
        double discountedmNI = (mNI - ((mNI/100)*10)) * (mNIC - mNIC2);
    
    
        if(mNIC >= 10){
    
            System.out.println(discountedmNIP + " " + (mNIC - mNIC2) + " " + discountedmNI );
            System.out.println(mNI + " " + mNIC2 + " " + mNIC2disc);
    
        }
    
    
    
        else{
    
            System.out.print(mNI + " " + mNIC);
            System.out.print(mNI * mNIC);
    
    
        }
    
        double sum = (mNI + discountedmNI + discountedRh + rH);
    
        System.out.println('\t');
        System.out.println("Total order cost " + sum);
    

    All I need to do is to take the units % 10 which will divide the left side integer or double by the right side (left side input from user)

    and will give me the remainder when I do that variable subtracted to the original variable!

    Again, this small step took me a whole night to figure it out, and is simple indeed. This is for a class, and if you are in that class and you are reading (even though you might have to dig a little to find what assignment is this one), I would just like to tell you this is what's fun about programming! I am not being sarcastic I really love these type of problems!

    Signed:

    That foreign guy;

    EUREKA again!

    Enjoy!