Writing a small program that requires me to convert strings to doubles and then back to strings. Right now, the code compiles but the answer is wrong, even though I am using an algorithm that I know is right in C++. Can someone tell me what I'm doing wrong? The answer that comes out always very large and random.

else{
        
        //Converting Strings to Double INT's
        double myMonths = [monthsString doubleValue];
        double myBalance = [balanceString doubleValue];
        double myInterest = [interestString doubleValue];
        
        //Payment Algorithm
        myInterest = (myInterest/100)/12;
        double AlmostDone = (myInterest * pow(1 + myInterest, myMonths)) * (myBalance/(pow(1 + myInterest, myMonths) -1));
        
        //Converting a double INT back to a String
        NSString *FinalPayment = [NSString stringWithFormat:@"%d", AlmostDone];
    
        //Print to Screen
        NSString *paymentScreen = [[NSString alloc] initWithFormat:@"In order to pay your debt in %@ months, you need to pay an average of %@ per month.", monthsString, FinalPayment];
        self.label.text = paymentScreen;
    }

Nevermind, all you have to is replace %d with %lf.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.