在我的应用中,我有一个金额字段。它应该只接受小数点之前的6位数和小数点后的任意位数,它将四舍五入为2位数。如何执行此操作。例如,在单击RETURN键时,键入12345678可以截断为123456.
相同的方式,
12345678.75 - > 123456
123456.456 - > 123456.46
答案 0 :(得分:3)
此代码应该有所帮助。
double input = 12345678.75;
double output;
NSString *string = [NSString stringWithFormat:@"%lf", input];
int dotPosition = [string rangeOfString:@"."].location;
if(dotPosition == NSNotFound || dotPosition > 6)
{
output = [string substringToIndex:6].doubleValue;
}
else
{
output = round(input * 100) / 100;
}
UPD根据iPhoneDev的评论:
上一部分代码接受并返回双值。
这是代码whitch与字符串交易:
NSString * input = @"123456.754";
NSString * output;
int dotPosition = [input rangeOfString:@"."].location;
if(dotPosition == NSNotFound || dotPosition > 6)
{
output = [input substringToIndex:6];
}
else
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.decimalSeparator = @".";
[formatter setRoundingMode: NSNumberFormatterRoundHalfUp];
[formatter setMaximumFractionDigits:2];
output = [formatter stringFromNumber:@(input.doubleValue)];
}
答案 1 :(得分:1)
使用此代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString* proposedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//if there is empty string return YES
if([proposedString length] == 0) {
return YES;
}
//create inverted set for appripriate symbols
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789.,"] invertedSet];
//if side symbols is trimmed by nonNumberSet - return NO
if([proposedString stringByTrimmingCharactersInSet:nonNumberSet].length != [proposedString length]) {
return NO;
}
if([[proposedString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]] count] > 2) {
return NO;
}
//finally check is ok, return YES
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSArray *components = [textField.text componentsSeparatedByString:@"."];
// Below 6 & 2 are the range
NSString *leftString;
if ([[components objectAtIndex:0] length] > 6) {
leftString = [[components objectAtIndex:0] substringToIndex:6];
}
NSString *rightString;
if ([[components objectAtIndex:1] length] > 2) {
rightString = [[components objectAtIndex:1] substringToIndex:2];
}
[textField setText:[NSString stringWithFormat:@"%@.%@",leftString,rightString]];
[textField resignFirstResponder];
return YES;
}
答案 2 :(得分:-1)
double input = 123456.4645;
double output;
NSString *string = [NSString stringWithFormat:@"%lf", input];
int dotPosition = [string rangeOfString:@"."].location;
if(dotPosition == NSNotFound || dotPosition > 6)
{
output = [string substringToIndex:6].doubleValue;
}
else
{
double Rate_int = [input doubleValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setRoundingMode: NSNumberFormatterRoundHalfUp];
[formatter setMaximumFractionDigits:2];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithDouble:Rate_int]];
output = [numberString doubleValue];
}