1.關(guān)于小數(shù)位末尾去0
最近開發(fā)遇到該需求册着,網(wǎng)上搜索發(fā)現(xiàn)邏輯太過復(fù)雜褐着,代碼量太多,打算自己封裝一下豆挽,分享給有需要的朋友
-(NSString*)deleteFloatAllZero:(NSString*)string
{
NSArray * arrStr=[string componentsSeparatedByString:@"."];
NSString *str=arrStr.firstObject;
NSString *str1=arrStr.lastObject;
while ([str1 hasSuffix:@"0"]) {
str1=[str1 substringToIndex:(str1.length-1)];
}
return (str1.length>0)?[NSString stringWithFormat:@"%@.%@",str,str1]:str;
}
2.關(guān)于判斷字符串是否為數(shù)字
有時候會遇到這種需求育谬,正常情況下,后臺傳過來的數(shù)子類型我們是可以直接轉(zhuǎn)換的帮哈,但有時也會莫名的傳來非數(shù)字類型膛檀,這時就要加判斷再做相應(yīng)的的處理。
- (BOOL)isAllNumberInString:(NSString *)str{
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
str=[str stringByReplacingOccurrencesOfString:@"." withString:@""];
if (str.length > 0) {
return NO;
}
return YES;
}