我有佔位符00.00的文本字段。我想以這種格式輸入數字。 請幫助ios中textfield中的十進制格式
-2
A
回答
0
使鍵盤類型數字。而在內部使用下面的代碼來獲得雙重價值
NSString *value =textfield_name.text;
double doubleValue = [value doubleValue];
0
試試這個方法:
讓您的課委託yourTextField
,並添加代碼從here使用。
NSString *[email protected]"99.129";//get your value from textfield as yourField.text
double doubleValue=[enteredValue doubleValue];
NSString *formattedDoubleValue=[NSString stringWithFormat:@"%.2f",doubleValue];
NSLog(@"->%@",formattedDoubleValue);//now you can set back to the textfield as yourField.text=formattedDoubleValue
或者,您可以使用NSNumberFormatter。
NSNumberFormatter *numFormatter=[NSNumberFormatter new];
[numFormatter setFormat:@"00.00"];
NSString *str=[numFormatter stringFromNumber:@99.129];
NSLog(@"->%@",str);
編輯:按照您的評論
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.yourTextField){
NSLog(@"here");
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^([0-9]{1,2})(\\.([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches==0){
return NO;
}
}
return YES;
}
相關問題
- 1. 十進制格式
- 2. Jframe中的十進制格式?
- 3. Java中的十進制格式?
- 4. 在Java中的十進制格式
- 5. 在mySQL中格式化十進制?
- 6. 在Javascript中格式化十六進制
- 7. 十六進制格式在Python中
- 8. 在powershell中驗證十進制格式
- 9. XSL十進制格式
- 10. 十進制格式在C#
- 11. 十六進制格式
- 12. 十進制格式apache poi
- 13. 十六進制格式
- 14. 格式化十進制數
- 15. java.lang.IllegalArgumentException十進制格式
- 16. 數字十進制格式
- 17. c#textbox十進制格式
- 18. 更改十進制格式
- 19. 將十進制轉換爲特定格式的十進制數?
- 20. 格式在UITextfield中有兩個十進制格式的整數
- 21. 模式的十進制格式
- 22. iOS中的十進制問題
- 23. 以十六進制或十進制格式打印變量
- 24. 以十進制和浮點格式打印十六進制值
- 25. 將十六進制數轉換爲十進制格式
- 26. 十進制格式忽略十進制0
- 27. 有條件的十進制格式
- 28. 貨幣的十進制格式
- 29. ListView列的兩個十進制格式
- 30. 帶十進制格式的C#string.format。
你想進入或希望用戶輸入並在給定的格式驗證? – 2013-02-25 09:55:02
[當用戶輸入輸入時驗證數字輸入到UITextField]的可能重複(http://stackoverflow.com/questions/9344159/validate-numeric-input-to-uitextfield-as-the-user-enters-input) – trojanfoe 2013-02-25 10:09:44
感謝trojanfoe – Deepak 2013-02-25 10:19:44