Link to home
Create AccountLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Comparing 3 amounts (can't be that hard!)

This can't be that hard but not sure why I can't get it

I have 3 amounts

amount1 = 12.7
amount2 = 33.8
amount3 = 40.99

I want to compare the 3 and find the highest one. How can I do that?? I know one way is use if statement. Not sure how to do this!!
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

            double amount1 = 12.7;
            double amount2 = 33.8;
            double amount3 = 40.99;

            double highest = (new double[] { amount1, amount2, amount3 }).OrderByDescending(n => n).First();

Open in new window

u need to add:
using System.Linq;

Open in new window

Avatar of Camillia

ASKER

I cant use linq in this app. Is there a way to do it the old fashioned way...using if else, or something similar?

it's easy with 2 values...

if (amount1 > amount2)
   whatever;

but not sure how to do it with 3 amounts
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
yes, Math.Max works. One more question ( i can close this and open a new one, if i need to).

So, now I have the highest, but how can I tell which if-statement it's coming from. So, this is what I have:

string paymentMethod = string.Empty;
string method = string.Empty;

if( wahtever) )
    	{
    		paymentMethod = "CARD";
    		amount1 = 55.00;
    	}
    	
    	if( whatever2) )
    	{
    		paymentMethod = "GIFT";
    		amoutn2 = 44.33;
    	}
        
    	if( whatever3)
    	{
    		paymentMethod = "PAYPAL";
    		paypalAmount = 37.00
    	}

double highest = Math.Max(Math.Max(amount1 , amount2 ), amount3 ); 

//now, I have the highest, how can I set method = paymentMethod which is in this case, it's "CARD"

Open in new window