我在浮点数中有值25.00,但当我在屏幕上打印它时,它是25.0000000。 如何显示只有两位小数点后的值?


这不是数字如何存储的问题,而是如何显示它的问题。当将其转换为字符串时,必须四舍五入到所需的精度,在您的示例中是小数点后两位。

例如:

NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloat];

%.02f告诉格式化程序,您将格式化一个浮点数(%f)和,它应该四舍五入到两个位置,并且应该用0填充。

例如:

%f = 25.000000
%.f = 25
%.02f = 25.00

在objective-c中,如果你正在处理常规字符数组(而不是指向NSString的指针),你也可以使用:

printf("%.02f", your_float_var);

OTOH,如果你想把这个值存储在一个char数组中,你可以使用:

sprintf(your_char_ptr, "%.02f", your_float_var);

在objective -c中,你想显示2个十进制数的浮点值,然后传递参数指示你想显示多少个小数点 例如0.02f将打印25.00 0.002f将打印25.000

你也可以尝试使用NSNumberFormatter:

NSNumberFormatter* nf = [[[NSNumberFormatter alloc] init] autorelease];
nf.positiveFormat = @"0.##";
NSString* s = [nf stringFromNumber: [NSNumber numberWithFloat: myFloat]];

你可能还需要设置否定格式,但我认为解决这个问题已经足够聪明了。

这里有一些更正

//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"

等等……

如果你也需要浮动值:

NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloat];
float floatTwoDecimalDigits = atof([formattedNumber UTF8String]);

下面是一些根据精度动态格式化的方法:

+ (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(取整)

 lblMeter.text=[NSString stringWithFormat:@"%.02f",[[dic objectForKey:@"distance"] floatValue]];

在Swift语言中,如果你想要显示你需要这样使用它。要在UITextView中赋值double,例如:

let result = 23.954893
resultTextView.text = NSString(format:"%.2f", result)

如果你想在LOG中显示,就像objective-c使用NSLog()一样,那么在Swift语言中你可以这样做:

println(NSString(format:"%.2f", result))

我根据上面的回答做了一个快速的扩展

extension Float {
    func round(decimalPlace:Int)->Float{
        let format = NSString(format: "%%.%if", decimalPlace)
        let string = NSString(format: format, self)
        return Float(atof(string.UTF8String))
    }
}

用法:

let floatOne:Float = 3.1415926
let floatTwo:Float = 3.1425934
print(floatOne.round(2) == floatTwo.round(2))
// should be true

Swift的另一个方法(不使用NSString):

let percentage = 33.3333
let text = String.localizedStringWithFormat("%.02f %@", percentage, "%")

附注:此解决方案不适用于CGFloat类型,仅用Float & Double测试

使用NSNumberFormatter和maximumFractionDigits,如下所示:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:12.345]]);

你会得到12.35

所有答案的问题是,乘法和除法会导致精度问题,因为你使用了除法。这是我很久以前在PDP8上编程时学到的。 解决方法是:

return roundf(number * 100) * .01;

因此,15.6578只返回15.66,而不是15.6578999或类似意外的结果。

你想要的精确程度取决于你自己。只是不要除以乘积,要乘以等量的小数。 不需要搞笑的字符串转换。