Converting Decimal to String to Decimal - objective-c

I copied this code from another post. I tried the example, however, I am getting a EXEC_BAD_ACCESS. From what I have read, this error happens when trying to use an object that has been deallocated, but I just don't see where I am doing that:
The call
...
float weighted_average = num_of_passes / total_of_all_passes;
NSString *newNumber = [[NSString alloc] init];
newNumber = [self formattedStringWithDecimal:weightedAverage]; //weighted average (float) = 15.875145
...
The Function
- (NSString *)formattedStringWithDecimal:(NSDecimalNumber *)decimalNumber
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2]; //two deimal spaces
[formatter setRoundingMode: NSNumberFormatterRoundHalfUp]; //round up
NSString *result =[NSString stringWithString:[formatter stringFromNumber:decimalNumber]];
[formatter release];
return result;
}

Call the method like this:
newNumber = [self formattedStringWithDecimal:[NSDecimalNumber numberWithFloat:15.434]];
You've tried to pass a primitive, but the method expects an object: an NSDecimalNumber. You've got to use the static convenience method numberWithFloat to create an object of that type.
And by the way, I have the feeling that
newNumber = [NSString stringWithFormat#"%.2f", 15.434];
could achieve the same result with less lines of code. Note this will not round up your number though.

You are returning an autoreleased object. Does the function that uses it retain it? If not, it could be released and then later (later run loop) its trying to be (re)used. Agreed on the enabling zombies to spot that kind of thing.

Related

NSNumberFormatter: dispatch_once where Currency changes

I have a tableview where a list of NSDecimalNumber will be displayed with their own currencies.
I use a dispatch_once on a NSNumberFormatter but notice that sometimes it'll totally ignore my commands to set the fraction digits or suffix on fraction digits altogether.
Currently the only way I've been able to resolve this is to init the NSNumberFormatter every row which is a bit silly.
ie:
- (NSNumberFormatter *)currencyFormatWithCurrency:(NSString *)currency
{
static NSNumberFormatter *_formatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_formatter = [[NSNumberFormatter alloc] init];
});
[_formatter setCurrencyCode:currency];
[_formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_formatter setMaximumFractionDigits:0];
[_formatter setGeneratesDecimalNumbers:NO];
[_formatter setRoundingMode:NSNumberFormatterRoundUp];
return _formatter;
}
// someCurrency is a string that will change per row
// someAmount is a NSDecimalNumber that will change per row
NSNumberFormatter *formatter = [self currencyFormatWithCurrency:someCurrency];
NSString *formattedAmount = [formatter stringFromNumber:someAmount];
self.amountLabel.text = formattedAmount;
Sometimes with the above if I feed in a figure like 80000, it'll return 80,000.00 (sometimes it depends on the currency).
I guess I could just init the NSNumberFormatter and nil it on each row.
Perhaps I'm doing it wrong, but if there is perhaps a way to ensure that I don't need to keep creating the NSNumberFormatter to ensure it'll understand the rules I give it?
Many thanks
NSNumberFormatter is not thread safe. That will lead to all kinds of problems. (Well, the way you use it cannot really be thread safe, because you might be modifying it in one thread while another thread is using it, but just using an NSNumberFormatter from two threads is not safe).
You can always do something in a method like
NSString* lastCurrency = nil;
NSNumberFormatter* formatter = nil;
for (;;) {
...
NSString* currency = ...;
if (! [lastCurrency isEqualToString:currency]) {
lastCurrency = currency;
formatter = ...;
}
}
Works well if in most cases the currency is unchanged.
I think this is a possible solution which I got from the following website might help things.
NSString *HTCurrencyString(double amount, NSString *currencyCode)
{
static NSString *currencyFormatterKey = #"HTCurrencyFormatter";
NSNumberFormatter *currencyFormatter = [[NSThread currentThread] threadDictionary][currencyFormatterKey];
if (currencyFormatter == nil)
{
currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setLocale:[NSLocale currentLocale]];
[[NSThread currentThread] threadDictionary][currencyFormatterKey] = currencyFormatter;
}
[currencyFormatter setCurrencyCode:currencyCode];
[currencyFormatter setMaximumFractionDigits:0];
[currencyFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
return [currencyFormatter stringFromNumber:#(amount)];
}

Convert array of NSStrings to array of formatted NSNumbers

I have an array of strings called valuesArray containing values like this: 2913451.0938
I am trying to format those numbers so that I can display them like this: 2,913,451.09
Using the following code I am able to read the values from the array and convert them to NSNumbers (num), and I am also able to create a formatter to define how I want my numbers to be displayed (formatter).
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSNumber *num = valuesArray[indexPath.row];
NSLog(#"num: %#",num);
NSLog(#"Formatter: %#",formatter);
NSString *forNum = [formatter stringFromNumber:num];
NSLog(#"FormattedNum: %#",forNum);
When I run the code and get to the line NSLog(#"FormattedNum: %#",forNum); I see that it prints null. What am I missing?
The problem in your code is that you retrieve an element from you array of strings valueArray but assign it to an NSNumber typed variable—while the object really is an NSString. When you pass it to the formatter it returns nil (even though it also might crash, it's just undefined behavior).
You have to convert the string to an NSNumber:
NSNumber *num = #([valuesArray[indexPath.row] doubleValue]);
I just checked this code with this value :
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSNumber *num = #(2913451.0938);
NSLog(#"num: %#",num);
NSLog(#"Formatter: %#",formatter);
NSString *forNum = [formatter stringFromNumber:num];
NSLog(#"FormattedNum: %#",forNum);
It worked fine, no error what so ever as you mentioned.
See the Output :
2013-02-28 21:35:44.417 BrowserModal[4861:403] num: 2913451.0938
2013-02-28 21:35:44.418 BrowserModal[4861:403] Formatter: <NSNumberFormatter: 0x100160840>
2013-02-28 21:35:44.419 BrowserModal[4861:403] FormattedNum: 29,13,451.09
Please clean your target and re-build.
And
Make sure valuesArray[indexPath.row] returns a boxed NSNumber object.
Do as : #([valuesArray[indexPath.row] doubleValue]);

NSNumberFormatter is not returning a sensible answer when converting a string

I have a UITextField, which I am trying to use a NSNumberFormatter with a NSNumberFormatterDecimalStyle to convert the text field into an NSNumber to initialise an object.
However when I do this, I can't get any sensible results out of the conversion. For the purpose of this example I have replaced the UITextField with a string, but I still get strange results. I am sure I am doing something daft, but any help would be appreciated.
//NSString * boardNumberText = self.txtBoardNumbs.text;
NSString * boardNumberText = #"42";
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * boardNumber = [formatter numberFromString:boardNumberText];
if ([boardNumber isEqual:nil]) {
NSLog(#"Number was null");
}
NSLog(#"Number was not null");
[self.board setNumber:boardNumber];
NSLog(boardNumberText);
NSLog([NSString stringWithFormat:#"%d", boardNumber]);
NSLog([NSString stringWithFormat:#"%d", self.board.number]);
The output that I get from the log file when I run this is:
2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] Number was not null
2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] 42
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135821152
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135820272
You can't just log NSNumber to the console using the integer format specifier, since NSNumber is a wrapper object. Also, NSLog takes a format string, so you don't need to use stringWithFormat:. Try this instead:
NSLog(#"%d", [boardNumber intValue]);
Using
NSLog(#"%d", boardNumber);
is wrong: it prints the memory address of the boardNumber pointer (the NSNumber instance itself). Use
NSLog(#"%d", [boardNumber intValue]);
instead.
P. s.: that
NSLog([NSString stringWithFormat:...]);
is unnecessary and ugly; NSLog has built-in format string handling, use it!

Getting nil from NSNumberformater numberFromString

I'm trying to format an amount from a .txt file coming in es_US locale(x,xxx.xx), to my current locale with is es_ES(x.xxx,xx). I would expect that [NSNumberFormater numberFromString] would just reformat the string, however and I'm only getting a nil value from this method.
I also tried another approach after checking the answers from here, but NSDecimalnumber does not work if the string has thousand separators, so if anybody could tell me what am I doing wrong please...
- (void) setSaldo_sap:(NSString *)saldo_sap
{
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setLocale:[NSLocale currentLocale]];
[numFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numFormatter setNegativeFormat:#"-¤#,##0.00"];
//saldo_sap = #" -324,234.55"
NSString * tmpString = [saldo_sap stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSNumber *num = [numFormatter numberFromString:tmpString];
NSDecimalNumber *tempNumber = [NSDecimalNumber decimalNumberWithString:tmpString];
_saldo_sap = [numFormatter stringFromNumber:tempNumber];
}
I think you misinterpret the aim of NSNumberFormatter: it doesn't "reformat", it "formats" and "parses" a numbers formatted along the set rules. So if you have numbers coming in "es_US" locale but want to format them using "es_ES" you will need two NSNumberFormatters: one for each locale.
Parse the incoming number with "es_US" and format using "es_ES", simplifying a bit (I don't know those two locales and the exact format of your numbers so you may need to tweek it a bit):
NSString * tmpString = ...
NSNumberFormatter *usFormatter = [[NSNumberFormatter alloc] init];
[usFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier: #"es_US"] autorelease]];
[usFormatter setHasThousandSeparators: YES];
NSNumberFormatter *esFormatter = [[NSNumberFormatter alloc] init];
[esFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier: #"es_ES"] autorelease]];
[esFormatter setHasThousandSeparators: YES];
// this assignment looks also somewhat weird: is it an instance variable?
// 'cause if it is and you assign an autoreleased string you'll have a bad pointer there
_saldo_sap = [esFormatter stringFromNumber: [usFormatter numberFromString: tmpString]];
// And unless you use ARC you leak your formatter on each call, so at the end
[usFormatter release];
[esFormatter release];
EDIT
If the input strings contain prefix/postfix characters, that may prevent NSNumberFormatter to work (it use usually pretty strict), use setLenient::
"Sets whether the receiver will use heuristics to guess at the number which is intended by a string."
If you have more than one number to be converted, do not create the formatters for each number, this is just a waste of memory and cpu. Make them instance variables and reuse. It will be much clearer than just having one formatter and reconfiguring it between parsing one format and formatting in another.
NSString *_saldo_sap = #" -324234.55";
//NSString *_saldo_sap = #" 324,234.55";
NSString * tmpString = [_saldo_sap stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//tmpString = #"-324,234.55"
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
NSNumber *num = [numFormatter numberFromString:tmpString];
[numFormatter setLocale:[NSLocale currentLocale]];
[numFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numFormatter setNegativeFormat:#"-¤#,##0.00"];
_saldo_sap = [numFormatter stringFromNumber:num];
Firstly, for getting NSNumber from NSString the string must be in correct readable format i.e. it must not include any characters like " , " as stated.
Secondly, you must first convert the string to NSNumber and then format it accordingly.

Returning a string

I have just started with Objective-C and have a (probably) very, very basic question/problem.
int testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[[NSDate] dateByAddingTimeInterval:(60*x)];
return infstr;
}
NSString *testString=testf(1);
I currently have the following problem: I don't know how to return a string from a function. I couldn't even figure it out through Google.
The aforementioned Code leads to a warning "Pointer from integer without a cast". I hope anyone does know a solution to this "problem" and is willing to share it with me.
Thanks in advance.
You need to create an NSString object using the int.
NSString *testf(int x) {
return [NSString stringWithFormat:#"%i",x];
}
NSString *testString = testf(1);
Just change your return type from int to an NSString pointer like so:
NSString* testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[NSDate dateByAddingTimeInterval:(60*x)]];
[dateFormatter release];
return infstr;
}
NSString *testString=testf(1);
Also your dateFormatter is leaking, so add the release statement as shown above.