如何实现后台返回的数据有.00或.0,显示时整数不显示小数点及以后0,小数显示小数点
原创
©著作权归作者所有:来自51CTO博客作者拔山河的原创作品,请联系作者获取转载授权,否则将追究法律责任
如何实现后台返回的数据有.00或.0,显示时整数不显示小数点及以后0,小数显示小数点
解决方案,识别出这种整数,把它截去:
-(NSString *)getIntStrWithStr:(NSString *)str
{
if(isCommonUnitEmptyString(str))
{
return @"";
}
if([str rangeOfString:@"."].location == NSNotFound)
{
return str;
}
NSInteger i = str.length-1;
for(; i > 0; i--)
{
NSString *str1 = [str substringWithRange:NSMakeRange(i, 1)];
if(!([str1 isEqualToString:@"0"] || [str1 isEqualToString:@"."]))
{
break;
}
}
if(i+1 == str.length)
{
return str;
}
else
{
return [str substringToIndex:(i+1)];
}
}
//判断字符串为空
static inline BOOL isCommonUnitEmptyString(NSString *str) {
return ([str isKindOfClass:[NSNull class]] || str == nil || ![str isKindOfClass:[NSString class]] || [str length] < 1);
}
使用的方法
- (void)setModel:(LCNewYearDinnerModel *)model {
_model = model;
model.cost_price = [self getIntStrWithStr:model.cost_price];
model.price = [self getIntStrWithStr:model.price];
[self.originPrice setText:[NSString stringWithFormat:@"门店价格:¥%@", model.cost_price]];
[self.price setText:[NSString stringWithFormat:@"¥%@", model.price]];
}
后台返回的数据:
errcode = 1,
data = (
{
price = "1688.00",
cost_price = "1888.00",
use_start_time = "12.20",
use_end_time = "01.30",
suppliers_name = "兰小溪特色美食餐厅",
goods_name = "年夜饭",
goods_id = 132,
},
),