版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2017.06.25 |
前言
在app中,很多時候我們都有賬戶和余額系統(tǒng),要顯示余額很多都是與國際接軌,那就是三位一個逗號隔開,這里做的就是這個事情。感興趣的可以看看我寫的其他小技巧
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷
2. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯
3.實用小技巧(三):點擊手勢屏蔽子視圖的響應
4.實用小技巧(四):動態(tài)的增刪標簽視圖
5.實用小技巧(五):通過相冊或者相機更改圖標
6.實用小技巧(六):打印ios里所有字體
7. 實用小技巧(七):UITableViewCell自適應行高的計算
實現(xiàn)過程
??很多時候我們都需要余額顯示的數(shù)據(jù)是整數(shù)并且是三位就要一個逗號隔開,這里我就自己寫了兩個小的方法,實現(xiàn)的效果就是三位用一個逗號隔開的整數(shù)。
下面我就直接來代碼了,其他的就不寫了。
//這個方法實現(xiàn)的結果就是,將余額進行逗號分隔,三位一組
- (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];
}
//這個方法實現(xiàn)的是刪除余額里面的逗號,方便進行加減等數(shù)學邏輯的計算。
- (NSString *)deleteDotWithStr:(NSString *)str
{
NSMutableString *strM = [[NSMutableString alloc] initWithString:str];
[strM stringByReplacingOccurrencesOfString:@"," withString:@""];
return [strM copy];
};
實現(xiàn)效果
如下圖所示。

實現(xiàn)效果
??這個效果圖就是我用沙盒測試賬號充的虛擬幣,可以看見實現(xiàn)了分隔,我寫的這個簡單的方法只是到了12位,不過夠用了,也沒誰會充虛擬充1000億。方法寫著很簡單,寫著玩的,希望對大家有用。
后記
未完,待續(xù)~~~

我的,哈哈