显示价格,要求有小数,显示小数点后两位;没有小数,则不显示小数
/**
判断, 小数点的位置 跟总长度比较
9.99 1 4
9.990 1 5
总长度 - 小数点的位置 > 3 需要截取
*/
//4.1 查找小数点位置
//rangeOfString: 传入一个字符串, 返回对应的range(location, length)
NSInteger currentPriceLocation = [self.currentPriceLabel.text rangeOfString:@"."].location;
//4.2 判断有没有找到小数点位置
//NSNotFound = NSIntegerMax
if (currentPriceLocation != NSNotFound) {
//4.3 如果找到了小数点位置 总长度 - 小数点的位置 > 3 需要截取
if (self.currentPriceLabel.text.length - currentPriceLocation > 3) {
//4.4 重新赋值: 将字符串转换层 float 类型, 再赋值
self.currentPriceLabel.text = [NSString stringWithFormat:@"¥ %.2f", [dealModel.current_price floatValue]];
}
}
如果想要小数点后第一位不为零,小数点后第二位为0的数(如9.90),不显示小数点后为0的数(显示9.9),可以截取最后一位,如果是0则删除,不是0则保留