First off, I want to thank the developers and programmers on the site who have answered many of my questions in the past. It wasn't until today that I signed up at stackoverflow. The answers many of you have provided to others has helped me a lot! Thank you!
I have an app that I am working on that takes a daily rate of pay multiplies it by days worked and then by the federal tax percentage and gives the user a net income and a gross income amount.
The issue I am having is that I cannot figure out how to implement the '$" in my net & gross income textfields.
Secondly, I would like to express that I am a total NOOB at this, so I apologize that this question may render you to say "Ha, really Josh? This is coding for 9 year olds!" I copied some code from an earlier post and implanted it into mine, so it may seem off. If you can take a look at the code and tell me what I need to do, I would greatly appreciate it! Thanks!
#implementation CRPDViewController
#synthesize dailyRateTextField;
#synthesize daysWorkedTextField;
#synthesize grossIncomeTextField;
- (IBAction)calculateButtonPressed:(UIButton *)sender
{
int result = [dailyRateTextField.text intValue] * [daysWorkedTextField.text intValue];
grossIncomeTextField.text = [NSString stringWithFormat:#"%.d", result];
float taxRate = .72;
float grossPay = [self.grossIncomeTextField.text floatValue];
float netPay = grossPay * taxRate;
self.netIncomeTextField.text = [NSString stringWithFormat:#"%.f", netPay];
// alloc formatter
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
// set options.
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *amount = [NSNumber numberWithInteger:78000];
// get formatted string
NSString* netPay = [currencyStyle stringFromNumber:amount]
[currencyStyle release];
}
Please give explanations in laymen terms as much as possible! Thanks Again! :)
Joshua Hart
To set only the $ sign use:
[NSString stringWithFormat:#"$%.f", netPay];
and setting floating point to 2 decimal places try something like:
self.netIncomeTextField.text = [NSString stringWithFormat:#"$%.2f", netPay];
Related
I am having the same issues as here: Dynamically format a float in a NSString.
I have searched my hardest for the answer but everything I do seems to break it.
I have tried the following code:
cell.distanceLabel.text = [NSString stringWithFormat:#"%#km",[item objectForKey:#"distance"]];
the displayed value should only ever have two decimals but for some reason values between 1.00 and 9.99 display with more than two decimals.
Can anyone help me with what I am doing wrong here?
I believe you have to either use:
NSLog(#"THE LOG SCORE : %f", x);
OR you need to convert the float to a string like this:
NSString *myString = [[NSNumber numberWithFloat:myFloat] stringValue];
Not sure if this is possible, but for my app I would like to get a locale based string that describes a number.
For example, if I had the number 10,000,000.
In english, I would expect the phrase "Ten Million". However, in Hindi, it would be One crore. Is there any properties in NSNumberFormatter, or NSLocale that could help me with this?
I have checked the docs (NSNumberFormatter, NSLocale), and havent found what I'm looking for yet. Obviously I could write some code to handle these two cases, but I'd like a way that could work for any locale.
Edit: Thanks to leo for the answer! Here is a small snippet of code that will get anyone looking for the same thing started:
NSNumberFormatter formatter = [[NSNumberFormatter alloc] init];
[self.formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"hi_hi"];
[self.formatter setLocale:locale];
NSNumber * myNumber = [NSNumber numberWithInt:10000];
self.numberLabel.text = [self.formatter stringFromNumber:myNumber];
What you are looking for is the NSNumberFormatterSpellOutStyle style.
NSString* spelledOutString = [NSNumberFormatter localizedStringFromNumber:#10000000 numberStyle:NSNumberFormatterSpellOutStyle];
This question already has answers here:
Objective C Issue With Rounding Float
(4 answers)
Closed 9 years ago.
The APP I'm writting must do some financial calculations, basically it's all about credits, debits and the balance. The only problem (so far) is that if I calculate 999999999.99 - 0.00 the result is 1000000000.00. Please, does anyone know why that happens? Here's my code:
NSNumber *totalCredits;
NSNumber *totalDebits;
NSNumber *balance;
self.credits = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
totalCredits = [self.credits valueForKeyPath:#"#sum.amount"];
double totalCreditsDouble = [totalCredits doubleValue];
self.creditLabel.text = [NSString stringWithFormat:#"%1.2f", totalCreditsDouble];
self.debits = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
totalDebits = [self.debits valueForKeyPath:#"#sum.amount"];
double totalDebitsDouble = [totalDebits doubleValue];
self.debitLabel.text = [NSString stringWithFormat:#"%1.2f", totalDebitsDouble];
balance = [NSNumber numberWithFloat:([totalCredits floatValue] - [totalDebits floatValue])];
double balanceDouble = [balance doubleValue];
self.balanceLabel.text = [NSString stringWithFormat:#"%1.2f", balanceDouble];
All the data is stored as Double.
The C type double is generally not very well suited for financial calculations.
Foundation.framework has a good data type for that: NSDecimalNumber. NSDecimalNumber uses a decimal representation and has 36 digits of precision.
http://floating-point-gui.de/ is a good resource to start with. You are trying to store exact values using a datatype which cannot represent them with the accuracy or precision you need.
if the integrity of these calculations is important to you then you need to switch to storing these values in a format which accurately represents them.
In this case a simple solution might be to store all of your currencies in their smallest possible denomination (e.g. cents) as integers (assuming you don't need fractional cents and handle any division carefully and consistently).
In my project, I have a function that runs and spits out a 5 digit number into a label on my interface. The number can be a double or a float but I would like it to display like ###+##.##
Example Number 12345.67
Shown like 123+45.67
The output will always be a 5 digit number with decimals. I researched data formatting specifically number formatting but haven't come across anything specific for this case. This is where I change the number to a string and assign it to a label. Please help and thank you in advance for your time.
NSString *outputNumber = [NSString stringWithFormat:#"%f",numberHere];
_stationing.text = outputNumber;
There are a couple of ways this could be done.
NSMutableString *outputNumber = [[NSString stringWithFormat:#"%.2f", numberHere] mutableCopy];
[outputNumber insertString:#"+" atIndex:outputNumber.length - 5];
_stationing.text = outputNumber;
Another:
int high = numberHere / 100;
float low = numberHere - (high * 100);
NSString *outputNumber = [NSString stringWithFormat:#"%d+%.2f", high, low];
These approaches won't work well if the number is less than 100.
Hope this help
NSStirng *numberString = [NSString stringWithFormat:#"%.2f",numberHere];
NSString *firstPart = [numberString substringWithRange:NSMakeRange(0, 3)];
NSString *secondPart [numberString substringWithRange:NSMakeRange(3, 5)];
NSString *outputString = [NSString stringWithFormat:#"%#+%#", firstPart, secondPart];
If the number's format is not permanent, you should check the length first.
I want to know how to do some simple equations in my iOS app, if anyone could point me in the right direction that would be wonderful!
I need to know how to convert an NSNumber that represents minutes to an NSString which represents hours and minutes (example: 100 = 1 hour and 40 minutes)
I also want to know if its possible to convert something like 2013-02-08T10:50:00.000 to 10:50AM
Thanks for any tips you guys might have.
For the conversion :
NSNumber *yourNumber = [NSNumber numberWithInt100];
NSInteger hour = [yourNumber intValue] / 60;
NSInteger minutes = [yourNumber intValue] % 60;
NSString *time_stamp = [NSString stringWithFormat:#"%d hour and %d minutes",hour,minutes];
As the comments suggested you can use NSDateFormatter to format your NSDate.