Objective C 将 int 转换为 NSString (iPhone)

发布于 2024-08-23 18:59:52 字数 686 浏览 6 评论 0原文

我有以下代码,旨在将毫秒转换为小时、分钟和秒:

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);

相当简单。 Rawtime 是一个值为 1200 的 int。输出如下:

0:0:1

0x1.3eaf003d9573p-962:0x1.7bd2003d3ebp-981:-0x1.26197p-698

为什么将 int 转换为字符串会给出如此狂野的数字?我尝试过使用 %i 和 %u,但它们没有任何区别。怎么了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-08-30 18:59:52

您必须使用 %@ 作为 NSString 的转换说明符。将最后一行更改为:

NSLog(@"%@:%@:%@", hoursStr, minsStr, secsStr);

%a 意味着完全不同的东西。来自 printf() 手册页

aA
双参数被四舍五入并转换为样式中的十六进制表示法

<前><代码>[-]0xh.hhhp[+-]d

其中十六进制点字符后面的位数等于精度规范。

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.

染火枫林 2024-08-30 18:59:52

您应该使用 NSNumberFormatter 用于数字或 NSDateFormatter 用于日期/时间。这些数据格式化程序负责将格式本地化为用户的区域设置并处理各种内置格式。

为了您的使用,您需要将毫秒时间转换为 NSTimeIntervaltypedef 来自双精度):

NSTimeInterval time = rawtime/1e3;

现在您可以使用 NSDateFormatter显示时间:

NSDate *timeDate = [NSDate dateWithTimeIntervalSinceReferenceDate:time];
NSString *formattedTime = [NSDateFormatter localizedStringFromDate:timeDate
                                                         dateStyle:NSDateFormatterNoStyle 
                                                         timeStyle:NSDateFormatterMediumStyle];
NSString *rawTime = [[formattedTime componentsSeparatedByString:@" "] objectAtIndex:0];

在 OS X 上,最后一行删除“AM/PM”。这适用于小于 12 小时的任何时间,并会给出 HH:MM:SS 本地化格式的格式化字符串。在 iPhone 上,localizedStringFromDate:dateStyle:timeStyle: 尚不可用。您可以在日期格式化程序实例上使用 setTimeStyle:setDateStyle:stringFromDate: 实现相同的效果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文