nsformatter

Convert String to double when comma is used

别来无恙 提交于 2021-01-27 08:02:01
问题 I have a UITextfield and it is being populated by data from database. The value is formatted in a way that the fractional part is separated by comma. So, the Structure is something like 1,250.50 I save the data in a string and when I try to use the doubleValue method to convert the string to a double or floating number. I am getting 1. Here is my code. NSString *price = self.priceField.text; //here price = 1,250.50 double priceInDouble = [price doubleValue]; Here I get 1 instead of 1250.50. I

Convert String to double when comma is used

老子叫甜甜 提交于 2021-01-27 07:59:09
问题 I have a UITextfield and it is being populated by data from database. The value is formatted in a way that the fractional part is separated by comma. So, the Structure is something like 1,250.50 I save the data in a string and when I try to use the doubleValue method to convert the string to a double or floating number. I am getting 1. Here is my code. NSString *price = self.priceField.text; //here price = 1,250.50 double priceInDouble = [price doubleValue]; Here I get 1 instead of 1250.50. I

Swift convert Currency string to double

蓝咒 提交于 2020-01-23 01:39:18
问题 I have a string, "$4,102.33" that needs to be converted to double. This will always be US. My only way is a hack to strip out the $ and , and then convert to double. Seems like NSFormatter only lets me convert TO a currency and not from it. Is there a built-in function or better way than just removing the $ and , ? prior to converting it to double? 回答1: NumberFormatter can convert to and from string. Also Double cannot represent certain numbers exactly since it's based 2. Using Decimal is

How do I set a Custom NSFormatter to a NSTextView?

依然范特西╮ 提交于 2020-01-06 07:55:26
问题 I have a NSTextView and i have a custom NSFormatter that limits the string length. But how do i " [_textView setFormatter:customFormatter] " on a NSTextView ? I can't find how to do that. Example code would be appreciated! 回答1: It's not possible to use a custom NSFormatter with a NSTextView. (That appears to be the province of NSControl, of which NSTextField is a subclass.) However, it is possible to set up a delegate for your text view's text storage ( NSTextStorage ), and implement

dateFromString return nil after change 24 hour

做~自己de王妃 提交于 2019-12-19 10:12:24
问题 NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"hh:mm a"]; NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; if(sourceDate==nil) { NSLog(@"source date nil"); } I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ? 回答1: The locale needs to be set to avoid being affected by the 24-hour setting change. The format also has to

Doubts on concurrency with objects that can be used multiple times like formatters

主宰稳场 提交于 2019-12-19 07:55:32
问题 Maybe a stupid question to ask but I need some confirmations on it. Usually, when I deal with objects that can be used multiple times within my application I use an approach like the following. Create an extension, say for example NSDecimalNumber+Extension , or a class utility where a number formatter is created like the following. + (NSNumberFormatter*)internal_sharedNumberFormatter { static NSNumberFormatter* _internal_numberFormatter = nil; static dispatch_once_t onceToken; dispatch_once(

Doubts on concurrency with objects that can be used multiple times like formatters

笑着哭i 提交于 2019-12-19 07:55:07
问题 Maybe a stupid question to ask but I need some confirmations on it. Usually, when I deal with objects that can be used multiple times within my application I use an approach like the following. Create an extension, say for example NSDecimalNumber+Extension , or a class utility where a number formatter is created like the following. + (NSNumberFormatter*)internal_sharedNumberFormatter { static NSNumberFormatter* _internal_numberFormatter = nil; static dispatch_once_t onceToken; dispatch_once(

How do I use an NSFormatter subclass with an NSPopUpButton

痞子三分冷 提交于 2019-12-13 03:06:39
问题 I want to use an NSFormatter subclass to format the contents of an NSPopUpButton I'm using to display a list of choices. Basically I have an NSArray of MyObjects which is bound to the NSPopUpButton via the Content Values binding and I want to display something in the pop up menu other than what is returned by -description. I don't really want to use a transformer because then I have to create an entirely new array and transform each object into a string before adding it to the new array.

How to use println in Swift to format number

点点圈 提交于 2019-12-12 07:10:05
问题 When logging-out a float in Objective-C you can do the following to limit your output to only 2 decimal places: float avgTemp = 66.844322156 NSLog (@"average temp. = %.2f", avgTemp); But how do you do this in Swift? And how do you escape other characters in println in Swift? Here's a regular Swift println statement: println ("Avg. temp = \(avgTemp)") So how do you limit decimal places? Also, how do you escape double-quotes in println ? 回答1: Here's the shortest solution I found thus far: let

NSDateComponentFormatter Print 1 for NSTimeInvervals <= 60

别等时光非礼了梦想. 提交于 2019-12-12 01:54:48
问题 I'm trying to have create a timer application and utilize iOS8's NSDateComponentsFormatter. How can I output a properly-formatted string for NSTimeInterval values between 0 and 60? As seen in the Swift playground, it seems to be returning a value of 1. I'd like to return a string with a format seen on NSTimeIntervals > 60, "0:12". Note: I know that a I can manually take the time in seconds and calculate hour, minutes, etc. but that would not be using the NSDateComponentsFormatter. //