美文网首页
对字符串截取小数点后的指定位数(只舍不入)

对字符串截取小数点后的指定位数(只舍不入)

作者: 飞扬你我 | 来源:发表于2019-03-25 16:39 被阅读0次

    //对字符串截取小数点后的指定位数(只舍不入)  price:需要处理的数字,position:保留小数点第几位

    -(NSString*)interceptString:(float)price afterPoint:(int)position

    {

    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness: NO  raiseOnOverflow: NO  raiseOnUnderflow: NO  raiseOnDivideByZero: NO ];

    NSDecimalNumber*ouncesDecimal;

    NSDecimalNumber*roundedOunces;

    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];  roundedOunces = [ouncesDecimaldecimalNumberByRoundingAccordingToBehavior:roundingBehavior];

    return  [NSStringstringWithFormat:@"%@",roundedOunces];

    }

    相关文章

      网友评论

          本文标题:对字符串截取小数点后的指定位数(只舍不入)

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