在我的应用程序中,我有一个金额字段。它应该只接受小数点前的 6 位数字和小数点后的任意数字,这将四舍五入为 2 位数字。这个怎么做。?
例如,键入 12345678 可以在单击 RETURN 键时被截断为123456。
同样,
12345678.75 -> 123456
123456.456 -> 123456.46
问问题
150 次
3 回答
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 的评论:
代码的前一部分采用并返回双精度值。
这是处理字符串的代码:
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)];
}
于 2013-09-18T07:06:23.493 回答
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;
}
于 2013-09-18T07:10:53.840 回答
-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];
}
于 2013-09-18T08:56:36.573 回答