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)
Related
This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 6 years ago.
I'm rewriting a library from java which uses something like
public int myFunc(char c){
return c+200
}
because in java 'a' + 1 will be 'b'
in Swift I cannot simply add an Int to a Character. Google search doesn't know anything about that.
Swift is a Strong Type Language. So the typecast is strict.
If you get ASCII value, you can like this:
let asciiValues = string.utf8.map{ Int($0) }
I hope that helps.
This question already has answers here:
How to add percent sign to NSString
(7 answers)
Closed 6 years ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to get string like "99.99%" from Double,and I used format to get this:
let rate = 99.99999
let str = String(format: "%.2f%", rate)
// output 99.99
And \% is not allowed. So how to add percent sign in string format, please help me!
write % twice:
rate = 99.99
let str = String(format: "%.2f%%", rate)
The % symbol has a special use in a printf statement. How would you
place this character as part of the output on the screen?
You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.
Hope it help you :)
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 +
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.
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;