On 19 Jan 2009, at 00:21, Stuart Malin <stu...@zhameesha.com> wrote:
On Jan 18, 2009, at 1:06 AM, Rob Keniger wrote:

On 18/01/2009, at 11:18 AM, Darren Stuart wrote:

Hi there, sorry for the real noobish question but I can't figure
this out or find an answer.

I have a variable called myMoney and its a NSDecimalNumber and I
want to set init it with the value of 23.30.

NSDecimalNumber *myMoney =  [[NSDecimalNumber alloc] init];

If this is the wrong place to post this sort of question please can
someone point to such a place.

Kind Regards

Darren

You can't find an answer - have you had a look at the documentation?
It's right there.

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html

Sometimes to the newbie it may seem astounding that experienced Cocoa
programmers can snap back with specific references. These are not
really that difficult to find. Might I suggest an address to keep in
mind for searching for information:

        http://developer.apple.com/cocoa

Then use the search box in the upper right.

In regard to the OP's query, I entered "creating a decimal number"
The lead result is the NSDecimalNumberHandler Class Reference, as a
PDF, as documented for iPhone, and as having the link Rob reported
above.

I too followed up on the example link which took me to the top of the NSDecimalNumber Class reference document. Scrolling down the first thing I came accross under Tasks was "Creating a Decimal Number" and just beneath that a link to
+ decimalNumberWithDecimal:
which looked exactly like what the OP was asking for, viz:
<>
Creates and returns an NSDecimalNumber object equivalent to a given NSDecimal structure.
+ (NSDecimalNumber *)decimalNumberWithDecimal:(NSDecimal)decimal>
</>

Ah, and look perhaps I don't need to use an alloc....
Now with all respect to the writers of this documentation, my first question is "what is a decimal structure?". Now I think I know what a "decimal structure" might be, namely a number written using the decimal notation - is that with or without decimal points?
I followed up on  NSDecimal and obtained:
<>
NSDecimal
Used to describe a decimal number.
typedef struct {
   signed int _exponent:8;
   unsigned int _length:4;
   unsigned int _isNegative:1;
   unsigned int _isCompact:1;
   unsigned int _reserved:18;
   unsigned short _mantissa[NSDecimalMaxSize];
} NSDecimal;
</>

Scareeeeeee!!!! and nothing like what I thought it might be.
Of course, I should have seen the bracketed NSDecimal in front of the "decimal" alerting me to the fact that this was either a cocoa object or C struct, and hence the use of the word "structure". Indeed the very use of an unsusual word like structure should have alerted me to the fact that this was not an english language construct that was being denoted but rather a "data structure" or more accurately a struct data type. But as you can see, even after a year and a half of looking through this documentation I am still able to make basic mistakes in literacy. I have had reason to think about this quite frequently and have come to the conclusion that there is nothing simple or straightforward about writing easy to use technical manuals. One of the main problems it seems to me occurs when I'm trying to get quickly to an answer and rather than read and digest every word of the manual (how many hours might that take?) I scan looking for key words that I hope will take me in the desired direction. It often seems to me that we fail to take human psychology sufficiently into account when designing such documents And somewhere in all that searching through a manual I think we can get waylaid by phoenomena not unlike those relating to "inattentional blindness" or "change blindness". For instance take a look at this video to see just how fragile is our ability to attend.

http://www.youtube.com/watch?v=voAntzB7EwE&eurl

and the set of videos here
http://viscog.beckman.uiuc.edu/djs_lab/demos.html

with discussions of the phenomena here
http://www.skepdic.com/inattentionalblindness.html
http://www.skepdic.com/changeblindness.html

and of the unwelcome effects of this in the inspection of complex systems
http://www.inspect-ny.com/vision/vision.htm

O.K. lets leave that and go back to see if we can find something that looks a bit more like the OP's number : 23.30.

The next heading beneath "Creating a Decimal Number" is "Initializing a Decimal Number" and head of the list is
<>
– initWithDecimal:
</>

That looks promising.. but
<>
initWithDecimal:
Returns an NSDecimalNumber object initialized to represent a given decimal.
- (id)initWithDecimal:(NSDecimal)decimal>
</>

No good: there's that NSDecimal again, but note that this time it is not described as a structure but as a "decimal" !!!!
Back to the top of the list:
<>
Initializing a Decimal Number
    * – initWithDecimal:
    * – initWithMantissa:exponent:isNegative:
    * – initWithString:
    * – initWithString:locale:
</>

But there is nothing here that looks anything like it would allow me to intialise with a numeric value like 23.30.
What about – initWithString: could we write something like this:
x = [decimalObject initWithString:"23.30"];

Looks good
<>
- (id)initWithString:(NSString *)numericString
</>

But to check that this is the method for us follow the link that comes with NSString and which takes us to the top of its class reference. Well there's a lot to get through there before we get to the list of Tasks and even then...

I'll stop here. My point is that learning a system via the manual is not the easiest thing in the world and just pointing people at specific pages in the manual though helpful is not always necessarily the best way of doing things. The fact is that Cocoa is a very sophisticated system which allows for very sophisticated programming usage and it is the manner of that usage which is frequently less than obvious in the documentation and which is frequently essential to one's ability to understand what is being described.

So what is a simple answer to the OP's question?
Does x = [decimalObject initWithString:"23.30"]; actually work?

Go into XCode and sdo XCode->New Project -> Cocoa project and modify main so it looks like this

int main(int argc, char *argv[])
{
NSDecimalNumber *myDecimalObj = [[NSDecimalNumber alloc]initWithString:@"23.30"]; NSLog(@"myDecimalObj doubleValue=%6.3f",[myDecimalObj doubleValue]);
        
}
and Run. Here is the log
2009-01-19 12:25:06.479 test[384:10b] myDecimalObj doubleValue=23.300

Yep. It works. Now I know. But is that what the OP was actually after or was the 23.3 simply an example of a numeric value? Even now I'm not clear whether I could create a decimal number with a double , e.g. something like
cGFloatValue = 23.3;
NSDecimalNumber *myDecimalObj = [[NSDecimalNumber alloc]initWithDouble: cGFloatValue];

Quickly returning to the NSDecimalNumber documentation I see nothing obvious, not without a few contortions. Why is there no simple initialisation of this kind I wonder? So why would one want to use NSDecimalNumber anyway, just as a means of converting string input or formatting strings in the output? No idea. Where would I use this "object-oriented wrapper for doing base-10 arithmetic"? Is it to handle really long numbers? e.g.
http://www.dzone.com/links/cocoa_tutorial_dont_be_lazy_with_nsdecimalnumber.html

But then
http://cocoawithlove.com/2008/05/square-root-numerical-fun-with.html
says "Sadly, NSDecimalNumber does not appear to be written for the mathematically or scientifically inclined. Its operators are mostly limited to basic arithmetic".

One of those imponderable mysteries I guess. But someone must have thought it worthwhile.... so why not say so in the documentation?

Julius



http://juliuspaintings.co.uk



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to