Objective C Convert int to NSString (iPhone) - iphone

I have the following code that is meant to convert milliseconds into hours, mins and seconds:
int hours = floor(rawtime / 3600000);
int mins = floor((rawtime % 3600000) / (1000 * 60));
int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000);
NSLog(#"%d:%d:%d", hours, mins, secs);
NSString *hoursStr = [NSString stringWithFormat:#"%d", hours];
NSString *minsStr = [NSString stringWithFormat:#"%d", mins];
NSString *secsStr = [NSString stringWithFormat:#"%d", secs];
NSLog(#"%a:%a:%a", hoursStr, minsStr, secsStr);
Fairly straightforward. Rawtime is an int with value 1200. The output is like this:
0:0:1
0x1.3eaf003d9573p-962:0x1.7bd2003d3ebp-981:-0x1.26197p-698
Why is it that converting the ints to strings gives such wild numbers? I've tried using %i and %u and they made no difference. What is happening?

You have to use %# as the conversion specifier for an NSString. Change your last line to:
NSLog(#"%#:%#:%#", hoursStr, minsStr, secsStr);
%a means something totally different. From the printf() man page:
aA
The double argument is rounded and converted to hexadecimal notation in the style
[-]0xh.hhhp[+-]d
where the number of digits after the hexadecimal-point character is equal to the precision specification.

Instead of rolling your own string formatting code, you should be using an NSNumberFormatter for numbers or an NSDateFormatter for dates/times. These data formatters take care of localization of format to the user's locale and handle a variety of formats built-in.
For your use, you need to convert your millisecond time into an NSTimeInterval (typedef'd from a double):
NSTimeInterval time = rawtime/1e3;
Now you can use an NSDateFormatter to present the time:
NSDate *timeDate = [NSDate dateWithTimeIntervalSinceReferenceDate:time];
NSString *formattedTime = [NSDateFormatter localizedStringFromDate:timeDate
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterMediumStyle];
NSString *rawTime = [[formattedTime componentsSeparatedByString:#" "] objectAtIndex:0];
on OS X where the last line removes the "AM/PM". This will work for any time less than 12 hrs and will give a formatted string in the localized format for HH:MM:SS. On the iPhone, localizedStringFromDate:dateStyle:timeStyle: isn't available (yet). You can achieve the same effect with setTimeStyle:, setDateStyle: and stringFromDate: on a date formatter instance.

Related

objective-c stringvalue from double

I have the following code:
double d1 = 12.123456789012345;
NSString *test1 = [NSString stringWithFormat:#"%f", d1]; // string is: 12.123457
NSString *test1 = [NSString stringWithFormat:#"%g", d1]; // string is: 12.1235
How do I get a string value that is exactly the same as d1?
It may help you to take a look at Apple's guide to String Format Specifiers.
%f 64-bit floating-point number
%g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
Also read up on floating point (in)accuracy, and, of course What Every Computer Scientist Should Know About Floating-Point Arithmetic.
If you really want the string to match the double exactly, then use NSString to encode it and call doubleValue when you want the value. Also take a look at NSNumberFormatter.
How about
NSString *test1 = [NSString stringWithFormat:#"%.15f", d1];
Or simply go for the double as
NSString *test1 = [NSString stringWithFormat:#"%lf", d1];

Removing characters after the decimal point for a double

How can I remove the all the characters after the decimal point.
Instead of 7.3456, I would just like 7.
This is what I do to get the number so far with decimal places.
[NSString stringWithFormat:#" %f : %f",(audioPlayer.currentTime),(audioPlayer.duration) ];
Many Thanks,
-Code
You can specify what you want using format string :
[NSString stringWithFormat:#" %.0f : %.0f", (audioPlayer.currentTime),
(audioPlayer.duration)];
If you want this for display, use an NSNumberFormatter:
double sevenpointthreefourfivesix = 7.3456;
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
NSLog(#"%#", [formatter stringFromNumber:[NSNumber numberWithDouble:sevenpointthreefourfivesix]]);
2011-12-20 20:19:48.813 NoDecimal[55110:903] 7
If you want a value without the fractional part, use round(). If you want the closest integer value not greater than the original value, use floor().
floorf() is the function you're looking for.
you are after
[NSString stringWithFormat:#" %.00f : %.00f",(audioPlayer.currentTime),(audioPlayer.duration) ];
When formatting float you can tell the precision by the number before the f
Cast to int:
[NSString stringWithFormat:#" %i : %i",(int)(audioPlayer.currentTime),(int)(audioPlayer.duration) ];
Casting like this always rounds down (eg: just removes everything after the decimal place). This is what you asked for.
In the case of rounding to the NEAREST whole number you want to add 0.5 to the number
[NSString stringWithFormat:#" %i : %i",(int)(audioPlayer.currentTime+0.5f),(int)(audioPlayer.duration+0.5f) ];
This will round to the nearest whole number. eg: 1.2 becomes 1.7 and casting to int makes 1. 3.6 becomes 4.1 and casting makes 4. :)
Why not just cast the audioPlayer.currentTime to an integer before you use stringWithFormat?
[NSString stringWithFormat:#"%d", (int)(audioPlayer.currentTime)];
All you need to do is type-cast the double to an int, like so: int currentTime_int = (int)audioPlayer.currentTime;.
You can use this same approach for the other variable.
Many of the shorter answers here will work correctly. But if you want your code to be really clear and readable, you might want to explicitly specify your desired conversion from float to int, such as using:
int tmpInt = floorf(myFloat); // or roundf(), etc.
and then separately specifying how you want the integer formated, e.g.
... stringWithFormat:#"%d", tmpInt ... // or #"%+03d", etc.
instead of assuming that an inline cast shows what you want.
You may also use
double newDvalue =floor(dValue);
it will remove all the decimals point
using %.0f for string format will be good also

WCF DateTime to NSDate

I am using a web services which returns a JSON WCF DateTime.
The string goes like \/Date(1316397792913+0800)\/
I am interested in extracting out the 1316397792 which is the time since 1st Jan 1970 in seconds. So that I can use the NSDate timeIntervalSince1970 method to get the present time. I cropped out the last 3 digits as it's in milliseconds and the timeIntervalSince1970 takes in seconds.
Here's what I am currently doing which does not work for dates somewhere before 2001 which has a time interval in ms since 1970 less than 10 characters.
NSString *dateString = #"\/Date(1316397792913+0800)\/";
NSLog(#"dateString :%#", dateString);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
[[dateString substringWithRange:NSMakeRange(6, 10)] intValue]];
NSLog(#"NSDate:%#", date);
dateString :/Date(1316397792913+0800)/
NSDate:2011-09-19 02:03:12 +0000
Therefore, I need a better work around playing with the dateString which does not assume everytime we will be feteching 10 characters.
You can simply get the substring between the index of ( and the index of +. Something like:
NSRange begin = [dateString rangeOfString:"("];
NSRange end = [dateString rangeOfString:"+"];
NSString* milliSecondsSince1970 = [dateString substringWithRange:NSMakeRange(begin.location + 1, end.location - begin.location - 1)];
P.S: Check for the one off error.

NSDate from stange looking string

I have an NSDictionary with the value for date as:
"Date":"/Date(1314313200000+0100)/",
How can I turn this into an NSDate as it contains strings lie "Date": and / ?
1314313200000 is milliseconds since the epoch-date (1970-01-01), and 0100 is the timezone. You need to parse this info out from the string and build a date from it. The NSScanner class is a good fit for parsing information out of strangely formatted text.
// Init a scanner with your date string
NSScanner* scanner = [NSScanner scannerWithString:#"Date:/Date(1314313200000+0100)/"];
// Skip everything up to a valid numeric character
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
intoString:NULL];
// Parse numeric value into a 64bit variable
long long milliseconds = 0;
[scanner scanLongLong:&milliseconds];
// Create a date instance from milliseonds sinve 1970-01-01
NSDate* date = [NSDate dateWithTimeIntervalSince1970:milliseconds / 1000.0];
If the time-zone is also important just skip the + sign and parse a new number.

how to store time in NSDate without date?

I have a timer in my app. When I click on exit buton then timer gets stop and stores value into the string in format of 01:15:55 . I have an array to store this string object.
What I want is , now I want to display these values by comparing to each other. So I think first I have to convert the string into the NSDate but I am having only time format and do not want to store date.
How can I accomplish this task ? any suggestion ?
EDITED : code
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started
myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart / 60) % 60;
myAppDelegate.hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (myAppDelegate.hours > 0)
{
result = [NSString stringWithFormat:#"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else
{
result = [NSString stringWithFormat:#"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];
}
NSString *tempDateString = [NSString stringWithFormat:#"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];
Now I want to convert tempDateString into the NSDate so I can compare with similar objects. Is it possible ?
Thanks...
Sounds like an NSTimeInterval might be more appropriate. This is just a floating-point value indicating a number of seconds (including fractional seconds). You can manually format a value like this into whatever string format you want with some simple division and remainder math. (NSDate will give you time intervals since a reference date or other dates if you want to use those to get the values.) You can store NSTimeIntervals as strings if necessary.
NSDateComponents is always a good choice when storing only parts of a date/time (or a timespan).
It also gives you easy access to time management methods via NSCalendar. Then (unlike using NSTimeInterval), you don't have to set up any of the math yourself, and it will all automagically localize.