I want to check that input string is number in ios [duplicate] - ios

This question already has answers here:
NSString is integer?
(5 answers)
Closed 8 years ago.
I want to check that input string is number, is there any way?
Basically strings are recieved from text field so need to check that whether the string in number or not.

There are many solutions discussed in StackOverflow, take a look at:
Finding out whether a string is numeric or not

NSString *yourString = #"...";
[yourString doubleValue] / [yourString floatValue] - this function will return the following:
The floating-point value of the receiver’s text as a double. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
you can change the keyboard input type of the text field and then you make sure that the input is valid:
yourTextField.keyboardType = UIKeyboardTypeDecimalPad;

Related

Localize file with property in swift [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 2 years ago.
I need to transfer the double variable to the localization file, if I write %d I can only pass int, but I need to pass the double
You can pass the double with format string %f. To restrict the number of digits after the decimal point you can format is like this %0.2f. This will allow only 2 digits after the decimal point.
Sample Code:
let num = 10.5
let outputStr = String(format:"I am printing %0.1f", arguments:[num])
print(outputStr)

How do you confirm a string only contains numbers in Swift? [duplicate]

This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +

NSString stringWithFormat less parameters [duplicate]

This question already has an answer here:
Multiple arguments in stringWithFormat: “n$” positional specifiers
(1 answer)
Closed 7 years ago.
We need to format a string, but for some localisations we won't output all parameters. But it seems that it doesn't work to output less parameters than passed:
NSString *string = [NSString stringWithFormat: #"%2$#", #"<1111>", #"<22222>"];
NSLog(#"String = %#", string);
Outputs
String = <1111>
although i output the second parameter.
Is this a bug or a feature?
according to the related industrial standard, IEEE specification:
When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.
which means in other words, you must use the first %1$# parameter in your string formatter somewhere before you address to use the second one – so, it is not a bug at all.

Displaying .0 after round numbers in Swift [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 8 years ago.
I’ve made a calculator using Doubles in Swift. The problem is that when I display the outcome it will display .0 at the end even if it’s a round number. I have tried the round() function but since it’s a double it still seems to always display .0 . In Objective-c i did this by typing:
[NSString stringWithFormat:#”%.0f”, RunningTotal]; //RunningTotal being the outcome
In this case there would be no decimals at all which there would if there stood #”%.3f” for example.
Does anyone know how to do this in swift? I’ve looked around on different forums but couldn’t find it anywhere... Thanks!
Your can do the same in Swift.
let runningTotal = 12.0
let string = String(format:"%.0f", runningTotal)
println(string) // Output: 12
Generally, this would round the floating point number to the next integer.
The %g format could also be used, because that does not print trailing
zeros after the decimal point, for example:
String(format:"%g", 12.0) // 12
String(format:"%g", 12.3) // 12.3
For more advanced conversions, have a look at NSNumberFormatter.

NSLocalizedString with superscript [duplicate]

This question already has an answer here:
Unicode not converted when displayed
(1 answer)
Closed 9 years ago.
How would you add superscript inside an NSLocalized string?
I'm trying to write a superscript 2, if I do it like this, it works:
[title setText:[NSString stringWithFormat:#"CO\u00B2 %#",NSLocalizedString(#"c04View01_title", #"Title for current page")]];
But if I add the superscript to the localized string, it doesn't work, it just interprets that as 5 characters:
"c04View01_title" = "CO\u00B2 PROGRAMMERS";
[title setText:NSLocalizedString(#"c04View01_title", #"Title for current page")]];
The problem happens, when the string with the superscript is between strings, so I need to split the string in two parts, but in some languages the superscripted string ends up at the end of the sentence.
Try using an upper-case 'U' for the backslash-escape, as per Apple's documentation:
"c04View01_title" = "CO\U00B2 PROGRAMMERS";
You can also just put the character directly in the strings file, un-escaped. There is no need to backslash-encode most characters.

Resources