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.
Related
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)
let myString = "☺️"
let emoji = "😀😁😂😃😄😅😆😇😈👿😉😊☺️😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷🙂🙃🙄🤔🙁☹️🤒🤕🤑🤓🤗🤐🤠🤤🤥🤧🤢🤡🤣"
let characterSet = CharacterSet(charactersIn: emoji)
let range = (myString as NSString).rangeOfCharacter(from: characterSet)
(myString as NSString).substring(with: range)
(range as NSRange).location
(range as NSRange).length
(myString as NSString).length
substring == myString
This code can be ran in Playgrounds. Try changing myString to be any emoji face.
I'm using NSString and NSRange here as their values are easier to demonstrate, but this has the exact same behaviour with a Swift String or Range.
When I set myString to most of the face emojis, the range comes back as having a length of 2, and the substring can be used appropriately elsewhere. With only 2 face emojis - the "smiling face" emoji and "frowning face" emoji, the range comes back as a length of 1. In all cases, the length of the string comes back as 2. The substring with the given range of 1 is incomplete, and you can see that comparing it back to myString, as an example of comparing it to itself, gives a result of false. The result for the range of those 2 emojis should be 2.
Interestingly, looking at the unicode spec, those 2 emojis have vastly differently unicode values to their neighbours.
This seems like it may be an iOS bug. I can't think of anything I could be personally doing incorrectly here, as it works with all other emoji.
Hardly an answer but to much to fit into a comment so bear with me :)
I don't know if you've already seen this but I think your problem is addressed in the Platform State of the Union talk from WWDC 2017 (https://developer.apple.com/videos/play/wwdc2017/102/) in the section about what is new in Swift 4.
If you look at the video at about the 23 minutes 12 seconds mark you'll see Ted Kremenek talk about how they've fixed separating unicode characters out as expected in Swift 4 using Unicode 9 Grapheme Braking.
Also, have a look at this question and answer.
Yes...Don't ask me in detail what all this means, but it seems as if they're working on it :)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to Multiply two big double values as follows.
long double lastKnownValue;
and for eg: when i multiply two values like
1000 x 1000,i am expecting result as 1000000.
But it gives Result as 1e+06.
How can make the result as 1000000?;
for small numbers it works perfect.
The %g and %lg format strings request scientific notation for large numbers, which is what you are getting. Try using %Lf (Are you sure it's an upper case "L" for long double? I thought it would be a lower-case L.) And if you don't want any digits after the decimal separator, use %.0Lf (Actually, I'm not sure of the order of the characters for specifying the number of decimal digits in a long double. It might be %L.0f, but I think the first version is correct.)
Instead of
long double
use
unsigned long long
eg: `
unsigned long long lastKnownValue;
unsigned long long currentValue = [mainLabel.text longLongValue];
NSLog(#"%llu",lastKnownValue);
//Set the new value to the main label
mainLabel.text = [NSString stringWithFormat:#"%llu",lastKnownValue];
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:
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;