OS X App converting textfield with comma causing issues with double value - swift

I am creating an OS X app and when I do this:
var tempVal = 10495.33
tempTextField.doubleValue = self.tempVal
It shows like this: 10,495.33. Notice the comma.
Now when I modify that value to 30,400.34 in the NSTextField and try to assign it back to the doubleValue things get messed up.
tempVal = tempTextField.doubleValue //Now this makes tempVal = 30 instead of 30,400.34
This is all because of the comma. Without the comma things are fine.
I know there is a bad fix where I just remove all commas from the number string but I feel like there is a better/correct way to do this.

The issue is most likely cause by by your system settings. Apparently (do not kwon why) you should enter the numbers in a US like format (using a "." as decimal separator), but the output to the textfield is depending on your system setting. So if you are in Europe most likely you use "," as a decimal separator
you can fix it as follows (you configure the format of the NSTextField, so no need to format the data you enter)
//This codes works in swift 4
//This should be your textfield. It is either programmed or you link it through storyboard
var myTextField = NSTextField()
//This is a formatter. You can set it up how to format the textfield
var myFormat = NumberFormatter()
myFormat.decimalSeparator = "."
myFormat.numberStyle = .decimal
//Now you connect the two
myTextField.formatter = myFormat

Related

Adding Ui labels values

I have been attempting to Add the values of some UI labels in Xcode, but with '+' there is an error and I have not found any other way yet to have it work.
Storyboard
ViewController.Swift
It‘s not clear in your question but I think you’re trying to change the text displayed by the UILabel. If this is the case, you can do this as following
myLabel.text = “myText”
or
myLabel.text += “Test”
You have to be more specific. I don’t really get what you want to add so I will assume.
If you want to add the Strings the UILabels contain, you should do as Bruce said above.
If you want to add the two strings into a variable you should do this.
Var outcome = label1.text + label2.text
The outcome would be the string of label one plus the string of label two connected.
Now if the labels contain numbers and you want to add those numbers you should do this. (Assuming they do contain Integers)
let num1 = label1.text.toInt()
let num2 = label2.text.toInt()
let outcome = num1 + num2
In your case it would be....:
Let sumOfIntegers = MathCriAValue.text.toInt() + MathCriBValue.text.toInt() + MathCriCValue.text.toInt() + MathCriDValue.text.toInt()

In Swift 3.0 How to make one character in a string move backward when you typing?

I am new in Swift.
I am trying to make a budget application. This app have a Calculator like keyboard. My idea is when users enter the money app will automatically add a decimal place for users.
For example, if you type 1230 it will give you 12.30 and type 123 it will display 1.23
I wrote a couple lines of code down below. The problem is it only can add decimal point after first digit it won't go backwards when you give more digits. It only can display as X.XXXXX
I tried solve this problem with String.index(maybe increase index?) and NSNumber/NSString format. But I don't know this is the right direction or not.
let number = sender.currentTitle!
let i: String = displayPayment.text!
if (displayPayment.text?.contains("."))!{
displayPayment.text = i == "0" ? number : displayPayment.text! + number
}
else {
displayPayment.text = i == "0" ? number : displayPayment.text! + "." + number
}
Indexing Strings in Swift is not as "straightforward" as many would like, simply due to how Strings are represented internally. If you just want to add a . at before the second to last position of the user input you could do it like this:
let amount = "1230"
var result = amount
if amount.characters.count >= 2 {
let index = amount.index(amount.endIndex, offsetBy: -2)
result = amount[amount.startIndex..<index] + "." + amount[index..<amount.endIndex]
} else {
result = "0.0\(amount)"
}
So for the input of 1230 result will be 12.30. Now You might want to adjust this depending on your specific needs. For example, if the user inputs 30 this code would result in .30 (this might or might not be what you want).

NSTextField: How to detect when a string is too long?

In my UI I have an NSTextField (used as a label). This text field's string value changes dynamically. I'd like to know when its string value is too long to display. I've configured the text field to truncate its contents:
myLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
which works fine but I need to know when truncation occurs because in that situation I need to do something else in my UI.
First of all, calculate number of character the label can take (determine the maximum number of characters a UILabel can take).
Suppose, the label takes x characters then check, if(x < label.text.characters.count) { //Do you work.}

Display certain number of letters

I have a word that is being displayed into a label. Could I program it, where it will only show the last 2 characters of the word, or the the first 3 only? How can I do this?
Swift's string APIs can be a little confusing. You get access to the characters of a string via its characters property, on which you can then use prefix() or suffix() to get the substring you want. That subset of characters needs to be converted back to a String:
let str = "Hello, world!"
// first three characters:
let prefixSubstring = String(str.characters.prefix(3))
// last two characters:
let suffixSubstring = String(str.characters.suffix(2))
I agree it is definitely confusing working with String indexing in Swift and they have changed a little bit from Swift 1 to 2 making googling a bit of a challenge but it can actually be quite simple once you get a hang of the methods. You basically need to make it into a two-step process:
1) Find the index you need
2) Advance from there
For example:
let sampleString = "HelloWorld"
let lastThreeindex = sampleString.endIndex.advancedBy(-3)
sampleString.substringFromIndex(lastThreeindex) //prints rld
let secondIndex = sampleString.startIndex.advancedBy(2)
sampleString.substringToIndex(secondIndex) //prints He

NSDecimalNumber and popping digits off of the end

I'm not quite sure what to call it, but I have a text field to hold a currency value, so I'm storing that as a NSDecimalNumber. I don't want to use the numbers & symbols keyboard so I'm using a number pad, and inferring the location of a decimal place like ATMs do. It works fine for entering numbers. Type 1234 and it displays $12.34 but now I need to implement back space. So assuming $12.34 is entered hitting back space would show $1.23. I'm not quite sure how to do this with a decimal number. With an int you would just divide by 10 to remove the right most digit, but that obviously doesn't work here. I could do it by some messy converting to int / 10 then back to decimal but that just sounds horrific... Any suggestions?
Call - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(id < NSDecimalNumberBehaviors >)behavior on it
How about using stringValue?
1) NSDecimalNumber to String
2) substring last
3) String to NSDecimalNumber
Below is an example for Swift 3
func popLastNumber(of number: NSDecimalNumber) -> NSDecimalNumber {
let stringFromNumber = number.stringValue //NSNumber property
let lastIndex = stringFromNumber.endIndex
let targetIndex = stringFromNumber.index(before: lastIndex)
let removed = stringFromNumber.substring(to: targetIndex)
return NSDecimalNumber(string: removed)
}
If your input number is a single digit, it would return NaN.
You could replace it to NSDecimalNumber.zero if you need.
It may works like delete button on calcultor.
It's not tested much.
If someone found another NaN case, please report by reply.