今天写一个印尼的项目遇到一个问题,本来在模拟器上和真机上运行的好好的,逻辑小数点和逗号的判断也都写好了,结果在提交项目之后,切换到印尼语言,突然发现都好都变成了小数点,小数点都变成了逗号。。。瞬间懵逼了。。。翻山倒海一顿海找以为是自己的问题没,结果最后发现在他们的语言里就是这个习惯,怎么办呢,而且还要加入千位分隔符。。。得想办法转过来啊,于是自己写了一个转换的方法。。。。
<code>
NSString *str1 = [NSString stringWithFormat:@"%@",number];
NSArray *moneys = [str1 componentsSeparatedByString:@"."];
if (moneys.count > 2) {
return str1;
}else if (moneys.count < 2) {
NSString *str = [NSString stringWithFormat:@"%@",str1];
if (str.length <= 0) {
return @"".mutableCopy;
}
NSString *tempRemoveD = [str stringByReplacingOccurrencesOfString:@"," withString:@""];
NSMutableString *stringM = [NSMutableString stringWithString:tempRemoveD];
NSInteger n = 2;
for (NSInteger i = tempRemoveD.length - 3; i > 0; i--) {
n++;
if (n == 3) {
[stringM insertString:@"," atIndex:i];
n = 0;
}
}
return stringM;
}
else {
NSString *str = [NSString stringWithFormat:@"%@",moneys[0]];
if (str.length <= 0) {
return @"".mutableCopy;
}
NSString *tempRemoveD = [str stringByReplacingOccurrencesOfString:@"," withString:@""];
NSMutableString *stringM = [NSMutableString stringWithString:tempRemoveD];
NSInteger n = 2;
for (NSInteger i = tempRemoveD.length - 3; i > 0; i--) {
n++;
if (n == 3) {
[stringM insertString:@"," atIndex:i];
n = 0;
}
}
if([stringM isEqualToString:@""]){
stringM = @"0".mutableCopy;
}
return [NSString stringWithFormat:@"%@.%@", stringM,moneys[1]];
}
</code>
这样就完美的解决了我项目中的问题,不知道适不适合大家,不适应的话可以做少许的修改,写的有点乱,不喜勿喷~~~
网友评论