Set Precision for NSNumber value

前端 未结 1 503
离开以前
离开以前 2021-01-24 12:49

I am having an issue with an NSNumber variable \"num\" that is currently containing 0.522000003606

I looked up several other issues on stackoverflow with others having t

1条回答
  •  借酒劲吻你
    2021-01-24 13:28

    Problem Solved!..... Thanks for everyones input but yea.... it was a string. Martin R, thanks for you debugging code. And understanding _NSCFString is really just a wrapper for an NSString but they were mainly the same.

    Here was my solution...

    nowValue outputs : 24.14295959472
    
    nowValue = [now objectForKey:@"value"];
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * nowNum = [f numberFromString:nowValue];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setMaximumFractionDigits:2];
    nowValueFormatted = [formatter stringFromNumber:nowNum];
    
    nowNum outputs : 24.14
    

    0 讨论(0)
提交回复
热议问题