1
您好,我需要格式化用户在应用程序中输入的十进制数字。使用NSNumberFormatter格式化十进制数字
100. = 100
90.00 = 90
90.1 = 90.1
90.22 = 90.22
90.227 = 90.23
我应该如何使用NSNumberFormatter来实现这一点。
我取得相同的,如果其他条件
if ([cell.textField.text rangeOfString:@"."].location != NSNotFound)
{
NSString *strAfterDecimal = [cell.textField.text componentsSeparatedByString:@"."].lastObject;
NSInteger count = [strAfterDecimal length];
if(count == 0)
{
customField.value = [cell.textField.text
stringByReplacingOccurrencesOfString:@"." withString:@""];
}
else if(count == 1 || count == 2)
{
if([strAfterDecimal integerValue] > 0)
{
customField.value = cell.textField.text;
}
else
{
customField.value = [cell.textField.text componentsSeparatedByString:@"."].firstObject;
}
}
else if(count > 2)
{
if([strAfterDecimal integerValue] > 0)
{
customField.value = [NSString stringWithFormat:@"%0.2f",[cell.textField.text floatValue]];
}
else
{
customField.value = [cell.textField.text componentsSeparatedByString:@"."].firstObject;
}
}
}
我需要consize的代码中使用这个自定义。至于根据客户我不能把额外的代码,如果苹果给出的默认功能做的事情
这是否解决了以下要求:“90.00 = 90”? – sketchyTech
@sketchyTech是的,它确实 – vadian
非常感谢@vadian。它拯救了我的一天。它按照我的要求工作。再次感谢。 :) –