美文网首页
iOS 取小数点后几位

iOS 取小数点后几位

作者: xu1Peng | 来源:发表于2021-04-22 11:29 被阅读0次

- (NSString*)notRounding:(id)priceafterPoint:(NSInteger)position {

    //生成format格式

    NSString*format = [NSStringstringWithFormat:@"%%.%ldf",(long)position];

    CGFloatvalue =0.;

    //string 和 number 兼容

    if([pricerespondsToSelector:@selector(doubleValue)]) {

        value = [pricedoubleValue];

    }

    NSString*number = [NSStringstringWithFormat:format,value];

// 去掉小数点后面的无效0

    NSString * outNumber = [NSString stringWithFormat:@"%@",[[NSDecimalNumber decimalNumberWithString:number] stringValue]];

    returnoutNumber;

}

相关文章

网友评论

      本文标题:iOS 取小数点后几位

      本文链接:https://www.haomeiwen.com/subject/iamxrltx.html