1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58
  • Your question it is unclear. Are you trying to create an integer or a float field? If it is a float field is there a fixed number of fraction digits or there can be any? Btw 10495 is an integer, is the comma a grouping separator or fraction separator? – Leo Dabus Feb 02 '18 at 22:43
  • it will always be a decimal that is why I am using a double – Nevin Jethmalani Feb 02 '18 at 22:44
  • Again 10495 should be 10 thousand or 10.495? – Leo Dabus Feb 02 '18 at 22:46
  • 10495 should be ten thousand four hundred and ninety five but because of the comma it is becoming 10. I don't know why it is chopping off everything after the comma. it should not be 10.495. When I add a number formatter to my text field, it is removing the comma when I do this `tempTextField.doubleValue = self.tempVal` which is fine but if I want to keep the comma is there any other way to do this? – Nevin Jethmalani Feb 02 '18 at 22:51
  • Why are you using doubleValue if you are interest in integer? – Leo Dabus Feb 02 '18 at 22:52
  • Sorry that is my mistake. I just added a decimal value. they are supposed to have decimals – Nevin Jethmalani Feb 02 '18 at 22:53
  • So no fraction digits should be allowed in your text field? – Leo Dabus Feb 02 '18 at 22:53
  • what are fraction digits? this should not be allowed `11/45` if this is a fraction digit – Nevin Jethmalani Feb 02 '18 at 22:54
  • Fraction digits are cents in a currency field – Leo Dabus Feb 02 '18 at 22:55
  • so this is an acceptable value `0.344859` this could be a value but it won't ever be `$0.4334` <- with dollar sign – Nevin Jethmalani Feb 02 '18 at 22:55
  • have you tried this https://stackoverflow.com/a/30154625/2303865 or this https://stackoverflow.com/a/28314223/2303865 ? – Leo Dabus Feb 02 '18 at 22:57
  • Note that some countries use a dot and others use a comma for fraction digits. Are you wishing to allow both of them depending on its locale? – Leo Dabus Feb 02 '18 at 22:58
  • I saw this but this issues seems like something that apple should have a native function for instead of basically converting by removing the comma. The reason I am saying this is because when I assign a double to a textfield it automatically adds the comma. I am not using a number formatter to display the comma in the text field. Yes, I know some countries use a comma instead of a decimal. – Nevin Jethmalani Feb 02 '18 at 23:00
  • What do you mean it automatically adds the comma? If you don't use number formatter to convert it to string it won't add anything. Edit your question and post your code – Leo Dabus Feb 02 '18 at 23:02
  • it does though. when I do this `tempTextField.doubleValue = 2394959.394` it will display with a comma as `2,394,959.394` – Nevin Jethmalani Feb 02 '18 at 23:03
  • try `tempTextField.stringValue = String(2394959.394)` – Leo Dabus Feb 02 '18 at 23:04
  • If you would like to format it properly use NumberFormatter – Leo Dabus Feb 02 '18 at 23:05
  • 1
    Why don't you add a number formatter to the text field? It will solve your problems. – Willeke Feb 03 '18 at 09:04

1 Answers1

0

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
Theo
  • 49
  • 4
  • 1
    I'm hitting the same issue and my system settings are set to US. The example string in the System prefs even shows a comma for the thousands separator and a decimal for the fraction separator. – user1118321 Mar 21 '21 at 19:15