How do I achieve the following conversion from double to a string:
如何实现从double到字符串的以下转换:
1.4324 => "1.43"
9.4000 => "9.4"
43.000 => "43"
i.e. I want to round to to decimal places but dont want any trailing zeros, ie i dont want
也就是说,我想四舍五入到小数点后几位,但是我不想要后面的零
9.4 => "9.40" (wrong)
43.000 => "43.00" (wrong)
So this code which I have now doesn't work as it displays excess zeros:
所以我现在的代码不起作用因为它显示了多余的0:
[NSString stringWithFormat: @"%.2f", total]
5 个解决方案
#1
24
Use NSNumberFormatter. See the Data Formatting Programming Guide's chapter on Number Formatters.
使用NSNumberFormatter。参见数据格式编程指南关于数字格式器的章节。
#2
35
You made simple mistake. This will work:
你犯简单的错误。这将工作:
[NSString stringWithFormat: @"%.2lf", total]
#3
4
The easiest way is to probably roll your own. I've had to do this in C before since there's no way to get the behavior you want with printf
formatting.
最简单的方法可能是自己卷。我之前在C语言中也做过,因为无法获得printf格式所需的行为。
It doesn't appear to be much easier in Objective-C either. I'd give this a try:
在Objective-C中看起来也没那么简单。我想尝试一下:
NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
while ([ftotal hasSuffix:@"0"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
if ([ftotal hasSuffix:@"."]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
or this (possibly faster) variant:
或者这个(可能更快)的变体:
NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
if ([ftotal hasSuffix:@".00"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-3];
} else {
if ([ftotal hasSuffix:@"0"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
}
The stringWithFormat
guarantees there will always be a ".nn"
at the end (where n
is a digit character). The while
and if
simply strip off trailing zeros and the trailing decimal if it was an integer.
stringWithFormat保证始终存在一个"。最后(n是一个数字字符)。如果是整数,则去掉末尾的0和末尾的小数。
Obviously, you may want to put it in a function or class so you can get at it from anywhere without having to duplicate the code all over the place.
显然,您可能想把它放在一个函数或类中,这样您就可以从任何地方得到它,而不必在整个地方复制代码。
#4
1
I'm not familiar with objective C, but this isn't possible with standard printf-style formatting.
我不熟悉objective - C,但是使用标准的printf样式格式是不可能的。
Using %g would sort-of work, but for large or small numbers it would use scientific notation (eg 9.6e+6, 4.2e-7) rather than decimal notation.
使用%g将是一种工作,但是对于大或小的数字,它将使用科学的表示法(如9.6e+6, 4.2e-7)而不是十进制表示法。
The equivalent question was asked for C/C++ here, and in that case the answer is to use %f and then strip any trailing 0's from the string. Not exactly elegant.
在这里,等效的问题被要求使用C/ c++,在这种情况下,答案是使用%f,然后从字符串中去掉任何尾随0。不是优雅。
#5
-2
I am pretty sure that [[NSNumber numberWithFloat:f] stringValue] does exactly what you want.
我很确定[NSNumber numberWithFloat:f] stringValue]正是你想要的。
#1
24
Use NSNumberFormatter. See the Data Formatting Programming Guide's chapter on Number Formatters.
使用NSNumberFormatter。参见数据格式编程指南关于数字格式器的章节。
#2
35
You made simple mistake. This will work:
你犯简单的错误。这将工作:
[NSString stringWithFormat: @"%.2lf", total]
#3
4
The easiest way is to probably roll your own. I've had to do this in C before since there's no way to get the behavior you want with printf
formatting.
最简单的方法可能是自己卷。我之前在C语言中也做过,因为无法获得printf格式所需的行为。
It doesn't appear to be much easier in Objective-C either. I'd give this a try:
在Objective-C中看起来也没那么简单。我想尝试一下:
NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
while ([ftotal hasSuffix:@"0"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
if ([ftotal hasSuffix:@"."]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
or this (possibly faster) variant:
或者这个(可能更快)的变体:
NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
if ([ftotal hasSuffix:@".00"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-3];
} else {
if ([ftotal hasSuffix:@"0"]) {
ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
}
The stringWithFormat
guarantees there will always be a ".nn"
at the end (where n
is a digit character). The while
and if
simply strip off trailing zeros and the trailing decimal if it was an integer.
stringWithFormat保证始终存在一个"。最后(n是一个数字字符)。如果是整数,则去掉末尾的0和末尾的小数。
Obviously, you may want to put it in a function or class so you can get at it from anywhere without having to duplicate the code all over the place.
显然,您可能想把它放在一个函数或类中,这样您就可以从任何地方得到它,而不必在整个地方复制代码。
#4
1
I'm not familiar with objective C, but this isn't possible with standard printf-style formatting.
我不熟悉objective - C,但是使用标准的printf样式格式是不可能的。
Using %g would sort-of work, but for large or small numbers it would use scientific notation (eg 9.6e+6, 4.2e-7) rather than decimal notation.
使用%g将是一种工作,但是对于大或小的数字,它将使用科学的表示法(如9.6e+6, 4.2e-7)而不是十进制表示法。
The equivalent question was asked for C/C++ here, and in that case the answer is to use %f and then strip any trailing 0's from the string. Not exactly elegant.
在这里,等效的问题被要求使用C/ c++,在这种情况下,答案是使用%f,然后从字符串中去掉任何尾随0。不是优雅。
#5
-2
I am pretty sure that [[NSNumber numberWithFloat:f] stringValue] does exactly what you want.
我很确定[NSNumber numberWithFloat:f] stringValue]正是你想要的。