舍入双精度值到2位小数

时间:2011-02-13 17:47:14

标签: iphone objective-c cocoa-touch

我有一个双倍值22.368511 我想把它舍入到2位小数。即它应该返回22.37

我该怎么做?

12 个答案:

答案 0 :(得分:186)

与大多数语言一样,格式为

%.2f

您可以看到更多示例here


编辑 :如果你担心在25.00的情况下显示该点,我也得到了这个

{
    NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
    [fmt setPositiveFormat:@"0.##"];
    NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.342]]);
    NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.3]]);
    NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.0]]);
}
2010-08-22 15:04:10.614 a.out[6954:903] 25.34
2010-08-22 15:04:10.616 a.out[6954:903] 25.3
2010-08-22 15:04:10.617 a.out[6954:903] 25

答案 1 :(得分:68)

您可以使用以下代码将其格式化为两位小数

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:22.368511]];

NSLog(@"Result...%@",numberString);//Result 22.37
斯威夫特4:

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.roundingMode = .up

let str = String(describing: formatter.string(from: 12.2345)!)

print(str)

答案 2 :(得分:27)

 value = (round(value*100)) / 100.0;

答案 3 :(得分:15)

[label setText:@"Value: %.2f", myNumber];

答案 4 :(得分:11)

您可以使用NSDecimalRound功能

答案 5 :(得分:11)

在Swift 2.0和Xcode 7.2中:

CORBA MARSHAL 0x4942f896 No; nested exception is: 
org.omg.CORBA.MARSHAL: Unable to read value from underlying bridge : purge_calls:1927 Reason: CONN_ABORT (1), State: ABORT (5)  vmcid: IBM  minor code: 896  completed: No

示例:

enter image description here

答案 6 :(得分:5)

要从double中删除小数,请查看此输出

对象C

double hellodouble = 10.025;
NSLog(@"Your value with 2 decimals: %.2f", hellodouble);
NSLog(@"Your value with no decimals: %.0f", hellodouble);

输出将是:

10.02 
10

Swift 2.1和Xcode 7.2.1

let hellodouble:Double = 3.14159265358979
print(String(format:"Your value with 2 decimals: %.2f", hellodouble))
print(String(format:"Your value with no decimals: %.0f", hellodouble))

输出将是:

3.14 
3

答案 7 :(得分:4)

我打算用Jason的答案,但我注意到在我的Xcode版本(4.3.3)中,我不能这样做。经过一些研究后,我发现他们最近改变了类方法并删除了所有旧方法。所以这就是我必须要做的事情:

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];

[fmt setMaximumFractionDigits:2];
NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.342]]);

答案 8 :(得分:2)

使用NSNumber * aNumber = [NSNumber numberWithDouble:number];而不是NSNumber * aNumber = [NSNumber numberWithFloat:number];

+(NSString *)roundToNearestValue:(double)number
{
    NSNumber *aNumber = [NSNumber numberWithDouble:number];

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setUsesGroupingSeparator:NO];
    [numberFormatter setMaximumFractionDigits:2];
    [numberFormatter setMinimumFractionDigits:0];
    NSString *string = [numberFormatter stringFromNumber:aNumber];        
    return string;
}

答案 9 :(得分:2)

对于Swift,有一个简单的解决方案,如果你不能导入Foundation,使用round()和/或不需要String(通常是你在Playground中的情况):

var number = 31.726354765
var intNumber = Int(number * 1000.0)
var roundedNumber = Double(intNumber) / 1000.0

结果:31.726

答案 10 :(得分:1)

我使用Umberto Raimondi发布的扩展Double类型的解决方案:

extension Double {
    func roundTo(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}

答案 11 :(得分:0)

你可以这样做:

{{1}}