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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:22:17+00:00 2026-05-31T14:22:17+00:00

Hey so I have test program for something I’m working on. What does it

  • 0

Hey so I have test program for something I’m working on.

What does it do? – It basically minuses the monthly loan payments from the total amount payable and then inserts it into an array, it is working however I need it to be formatted to 2 decimal places and I have no idea how to do that, I’ve tried using a method I found which is

[NSString stringWithFormat: @"%.2lf", tempPaymentHolder]

But it doesn’t seem to work, here is the code.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        //array that would hold the payments
        NSMutableArray * arrayWithPayments = [[NSMutableArray alloc]init];

        //total amount to be paid
        NSMutableString * totalPaymentString = [[NSMutableString alloc]initWithString:@"151.92"];

        //monthly payment
        NSMutableString * monthlyPaymentsString = [[NSMutableString alloc]initWithString:@"12.66"];

        //temp storage for passing values into array
        NSMutableString * tempPaymentStringHolder = [[NSMutableString alloc]init];
        tempPaymentStringHolder = [NSString stringWithFormat:@"%@",totalPaymentString];

        //doubles so I can calculate
        int numberOfMonths = 12;
        double monthlyPayments = [monthlyPaymentsString doubleValue];
        double totalPayment = [totalPaymentString doubleValue];
        double tempPaymentHolder = totalPayment;


        for (int i = 0; i <= numberOfMonths; i++) 
        {
            [arrayWithPayments addObject:tempPaymentStringHolder];
            tempPaymentHolder = totalPayment - monthlyPayments;
            totalPayment = tempPaymentHolder;
            tempPaymentStringHolder = [NSString stringWithFormat: @"%.2lf", tempPaymentHolder];
        }

        for (id obj in arrayWithPayments)
        {
            NSLog(@"%@",obj);
        }


    }
    return 0;
}

Would appreciate any help.

Thank you very much for your time.

  • 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-31T14:22:18+00:00Added an answer on May 31, 2026 at 2:22 pm

    Check out NSDecimalNumber:

    NSMutableArray *arrayOfPayments = [[NSMutableArray alloc]init];
    
    //total amount to be paid
    NSDecimalNumber * totalPayment = [NSDecimalNumber decimalNumberWithString:@"151.92"];
    
    //monthly payment
    NSDecimalNumber * monthlyPayments = [NSDecimalNumber decimalNumberWithString:@"12.66"];
    
    //temp storage for passing values into array
    NSDecimalNumber* tempPaymentHolder = [totalPayment copy];
    
    //variables so I can calculate
    int numberOfMonths = 12;
    
    
    for (int i = 0; i <= numberOfMonths; i++) 
    {
        [arrayOfPayments addObject:tempPaymentHolder];
        tempPaymentHolder = [totalPayment decimalNumberBySubtracting:monthlyPayments];
        totalPayment = [tempPaymentHolder copy];
    }
    
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    
    for (NSDecimalNumber *obj in arrayOfPayments)
    {
        NSLog(@"%@", [formatter stringFromNumber:obj]);
    }
    

    Output:

    $151.92
    $139.26
    $126.60
    $113.94
    $101.28
    $88.62
    $75.96
    $63.30
    $50.64
    $37.98
    $25.32
    $12.66
    $0.00

    Just be sure to remember to copy variables where you would normally assign them, as you are dealing with objects, not primitives here!

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

Sidebar

Related Questions

I have a string of test like this: <customtag>hey</customtag> I want to use a
Hey guys really quick question, I have a simple test that I was doing
If I have this code: array(0 => hey, 1 => bla, 2 => test,
I currently have something like this <p class=test></p> <script type=text/javascript> $(document).ready(function() { $(.test).html(hey); });
Hey, I have a small test case set up as following: class T {
hey guys, i can't seem to get it working. i have a div.post with
Hey, I have an iphone program that has a parser that parses some XML
Hey! I have a local test server on my machine and my production server.
If I have this array: array( hey , bla , test); and I want
Let's say I have this: test = function() { this.init = function() { $(#someElement).html(Hey!).mousedown(function()

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.