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.
Related
NSNumber * uniqueId = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970])];
Milliseconds is not included in the above code.If i used like below code ,it is printing negative values.
NSNumber * uniqueId1 = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970] * 1000)];
Can we get time stamp with milliseconds as int????
#AnjaniG you can use one of them..
NSString *strTimeStamp = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970] * 1000];
int timestamps = [strTimeStamp intValue];
NSLog(#"number int = %d",timestamps);
You should not use an Int, [[NSDate date] timeIntervalSince1970] * 1000 is bigger than INT_MAX. You should use long long to store the value.
NSNumber * uniqueId1 = [NSNumber numberWithLongLong:([[NSDate date] timeIntervalSince1970] * 1000)];
The problem is that you are casting a floating point to an integer.
As per the documentation, timeIntervalSince1970 returns an NSTimeInterval, which is a double, not an integer.
In your first code example, you're actually discarding the milliseconds by casting.
In your second code example, you are overflowing the integer. After multiplying the value by 1000, it is too large to fit in an integer.
In the end, you're just doing too much code and you shouldn't need to really worry about this.
NSTimeInterval interval = [NSDate date].timeIntervalSince1970;
NSNumber *timestamp = #(interval * 1000.);
Here, I use the proper documented type of NSTimeInterval. Then I multiply that by 1,000 to change seconds to milliseconds. Finally, I use Clang literal syntax to instruct the compiler to create the appropriate NSNumber.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 2 DATES.
End Date
Current Date
Now, I want to find NSTimeInterval and Calculate remaining time in Days, Hours, Minutes, Seconds.
I do not want to use NSDateComponents.
I want some formula that calculate that gives remaining time in Days, Hours, Minutes, Seconds.
I tried this below formula but that formula gives remaining Hours, Minutes, Seconds.
But how do I calculate this Days, Hours, Minutes, Seconds ??
I'm using this below code
#property (nonatomic, assign) NSTimeInterval secondsLeft;
_hours = (int)self.secondsLeft / 3600;
_minutes = ((int)self.secondsLeft % 3600) / 60;
_seconds = ((int)self.secondsLeft %3600) % 60;
Some of your calculations are incorrect. You want:
_hours = (int)self.secondsLeft / 3600;
_minutes = (int)self.secondsLeft / 60 % 60;
_seconds = (int)self.secondsLeft % 60;
This assumes _hours, _minutes, and _seconds are of type int (or some other appropriate integer type).
If you want to format the NSTimeInterval into a useful string formatted properly for the user's locale, use NSDateComponentsFormatter:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
NSString *result = [formatter stringFromTimeInterval:self.secondsLeft];
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];
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];
I'm calculating the average travel time from one point to another by using the following calculation:
float tSpeed = [[NSString stringWithFormat:#"%f", speed] floatValue];
float duration = totDistance/tSpeed;
tripDuration_Label.text = [NSString stringWithFormat:#"%f", duration];
speed is in miles per hour so the output is in hours.
This gives me a float value for the time, I need to convert it to time (hh:mm).
Thanks
1 hour is 60 minutes so:
NSInteger hours = duration; // 2.5 -> 2
NSInteger minutes = ( duration - hours ) * 60; // ( 2.5 - 2 ) * 60
tripDuration_Label.text = [NSString stringWithFormat:#"%d:%02d", hours, minutes];
Probably you would like to do the conversion to time like in this example but before you need to get the integer values from float.
Fot this you can use modulo to sparate things before and after comma.