我在浮点数中有值25.00,但当我在屏幕上打印它时,它是25.0000000。 如何显示只有两位小数点后的值?
当前回答
下面是一些根据精度动态格式化的方法:
+ (NSNumber *)numberFromString:(NSString *)string
{
if (string.length) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
return [f numberFromString:string];
} else {
return nil;
}
}
+ (NSString *)stringByFormattingString:(NSString *)string toPrecision:(NSInteger)precision
{
NSNumber *numberValue = [self numberFromString:string];
if (numberValue) {
NSString *formatString = [NSString stringWithFormat:@"%%.%ldf", (long)precision];
return [NSString stringWithFormat:formatString, numberValue.floatValue];
} else {
/* return original string */
return string;
}
}
如。
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:4];
=> 2.3453
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:0];
=> 2
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:2];
=> 2.35(取整)
其他回答
下面是一些根据精度动态格式化的方法:
+ (NSNumber *)numberFromString:(NSString *)string
{
if (string.length) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
return [f numberFromString:string];
} else {
return nil;
}
}
+ (NSString *)stringByFormattingString:(NSString *)string toPrecision:(NSInteger)precision
{
NSNumber *numberValue = [self numberFromString:string];
if (numberValue) {
NSString *formatString = [NSString stringWithFormat:@"%%.%ldf", (long)precision];
return [NSString stringWithFormat:formatString, numberValue.floatValue];
} else {
/* return original string */
return string;
}
}
如。
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:4];
=> 2.3453
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:0];
=> 2
[TSPAppDelegate stringByFormattingString:@"2.346324" toPrecision:2];
=> 2.35(取整)
如果你也需要浮动值:
NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloat];
float floatTwoDecimalDigits = atof([formattedNumber UTF8String]);
这里有一些更正
//for 3145.559706
斯威夫特3
let num: CGFloat = 3145.559706
print(String(format: "%f", num)) = 3145.559706
print(String(format: "%.f", num)) = 3145
print(String(format: "%.1f", num)) = 3145.6
print(String(format: "%.2f", num)) = 3145.56
print(String(format: "%.02f", num)) = 3145.56 // which is equal to @"%.2f"
print(String(format: "%.3f", num)) = 3145.560
print(String(format: "%.03f", num)) = 3145.560 // which is equal to @"%.3f"
卷-C
@"%f" = 3145.559706
@"%.f" = 3146
@"%.1f" = 3145.6
@"%.2f" = 3145.56
@"%.02f" = 3145.56 // which is equal to @"%.2f"
@"%.3f" = 3145.560
@"%.03f" = 3145.560 // which is equal to @"%.3f"
等等……
你也可以尝试使用NSNumberFormatter:
NSNumberFormatter* nf = [[[NSNumberFormatter alloc] init] autorelease];
nf.positiveFormat = @"0.##";
NSString* s = [nf stringFromNumber: [NSNumber numberWithFloat: myFloat]];
你可能还需要设置否定格式,但我认为解决这个问题已经足够聪明了。
在Swift语言中,如果你想要显示你需要这样使用它。要在UITextView中赋值double,例如:
let result = 23.954893
resultTextView.text = NSString(format:"%.2f", result)
如果你想在LOG中显示,就像objective-c使用NSLog()一样,那么在Swift语言中你可以这样做:
println(NSString(format:"%.2f", result))
推荐文章
- 如何设置回退按钮文本在Swift
- 如何比较两个nsdate:哪个是最近的?
- 如何用前导零格式化Java字符串?
- 使UINavigationBar透明
- Swift to Objective-C头未在Xcode 6中创建
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded
- 不区分大小写的比较
- 有人能解释一下c#中有符号浮点的这种奇怪行为吗?
- round()用于c++中的浮点数
- Java中的浮点和双数据类型
- 编译警告:对于架构i386,没有处理文件的规则
- 为什么浮点数不准确?
- 从ViewController调用App Delegate方法
- 从Pandas聚合结果中格式化/抑制科学符号
- 解释iOS7中automyadjustsscrollviewinsets, extendedlayoutinclesopaquebars, edgesForExtendedLayout之间的区别