版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.25 |
前言
在app中踊东,很多時(shí)候我們都有賬戶和余額系統(tǒng),要顯示余額很多都是與國際接軌刚操,那就是三位一個(gè)逗號(hào)隔開闸翅,這里做的就是這個(gè)事情。感興趣的可以看看我寫的其他小技巧
1. 實(shí)用小技巧(一):UIScrollView中上下左右滾動(dòng)方向的判斷
2. 實(shí)用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實(shí)用小技巧(三):點(diǎn)擊手勢(shì)屏蔽子視圖的響應(yīng)
4.實(shí)用小技巧(四):動(dòng)態(tài)的增刪標(biāo)簽視圖
5.實(shí)用小技巧(五):通過相冊(cè)或者相機(jī)更改圖標(biāo)
6.實(shí)用小技巧(六):打印ios里所有字體
7. 實(shí)用小技巧(七):UITableViewCell自適應(yīng)行高的計(jì)算
實(shí)現(xiàn)過程
??很多時(shí)候我們都需要余額顯示的數(shù)據(jù)是整數(shù)并且是三位就要一個(gè)逗號(hào)隔開菊霜,這里我就自己寫了兩個(gè)小的方法缎脾,實(shí)現(xiàn)的效果就是三位用一個(gè)逗號(hào)隔開的整數(shù)。
下面我就直接來代碼了占卧,其他的就不寫了。
//這個(gè)方法實(shí)現(xiàn)的結(jié)果就是联喘,將余額進(jìn)行逗號(hào)分隔华蜒,三位一組
- (NSString *)convertNSStringWithStr:(NSString *)str
{
NSMutableString *strM = [[NSMutableString alloc] initWithString:str];
if (strM.length < 4) {
}
else if (strM.length >= 4 && strM.length < 7){
NSInteger index = strM.length - 3;
[strM insertString:@"," atIndex:index];
}
else if (strM.length >= 7 && strM.length < 10){
NSInteger index1 = strM.length - 3;
[strM insertString:@"," atIndex:index1];
NSInteger index2 = strM.length - 7;
[strM insertString:@"," atIndex:index2];
}
else if (strM.length >= 10 && strM.length < 13){
NSInteger index1 = strM.length - 3;
[strM insertString:@"," atIndex:index1];
NSInteger index2 = strM.length - 7;
[strM insertString:@"," atIndex:index2];
NSInteger index3 = strM.length - 11;
[strM insertString:@"," atIndex:index3];
}
return [strM copy];
}
//這個(gè)方法實(shí)現(xiàn)的是刪除余額里面的逗號(hào),方便進(jìn)行加減等數(shù)學(xué)邏輯的計(jì)算豁遭。
- (NSString *)deleteDotWithStr:(NSString *)str
{
NSMutableString *strM = [[NSMutableString alloc] initWithString:str];
[strM stringByReplacingOccurrencesOfString:@"," withString:@""];
return [strM copy];
};
實(shí)現(xiàn)效果
如下圖所示叭喜。
??這個(gè)效果圖就是我用沙盒測(cè)試賬號(hào)充的虛擬幣,可以看見實(shí)現(xiàn)了分隔蓖谢,我寫的這個(gè)簡(jiǎn)單的方法只是到了12位捂蕴,不過夠用了,也沒誰會(huì)充虛擬充1000億闪幽。方法寫著很簡(jiǎn)單啥辨,寫著玩的,希望對(duì)大家有用盯腌。
后記
未完溉知,待續(xù)~~~