15 Oct 2015 0 comments

Unite is a conference that is run by Unity Technologies to promote their key product, the Unity gaming engine. It isn’t so much as a conference, but rather an experience. With a keynote being pre...

Read More
15 Oct 2015 0 comments

While I worked on creating the iOS Animations by Tutorials video series and the three editions of the iOS Animations by Tutorials book I got a lot of ideas how the existing anim...

Read More
15 Oct 2015 0 comments

Happy Wednesday – it’s time for our 3rd new book release as part of the iOS 9 Feast! Today, Aaron, Saul, Matthew, Pietro and I are happy to announce that Core Data by Tutorials Second E...

Read More
15 Oct 2015 0 comments

In this getting started with Core Data tutorial, you’ll write your very first Core Data app using Swift 2.0. You’ll see how easy it is to get started with all the resources provided in Xcode, fro...

Read More
15 Oct 2015 0 comments

When you create a Core Data app, you design an initial data model for your app. However, after you ship your app inevitably you’ll want to make changes to your data model. What do you do then – you...

Read More
15 Oct 2015 0 comments

As part of this year’s iOS 9 Feast, we are releasing a new video tutorial series every Thursday. This week, we are happy to release a brand new video tutorial series – Introducing Custom Cont...

Read More
15 Oct 2015 0 comments

There’s been a big hole in Spotlight on iOS for a long time. Although users can use it to find you app, they can’t see inside it — to all the content that they really care about. Currentl...

Read More
15 Oct 2015 0 comments

Storyboards have been around since iOS 5 and have received lots of upgrades and new features since then, including unwind segues for reverse navigation, universal storyboards for both iPhone and iP...

Read More

Latest Posts:

Objective C - Convert string to double and double to string




Before we go on to part 3, let me stop to pause for an important part of the code and zoom in a little more. For in the code, we have done something important, converting double into a string, and string into a double.

Now i initially thought that a simple google search would return me the result but to my horror...it doesn't! Especially for the double to format to 2 dp and become a string.

So here goes...

double loanAmount = [tbxLoanAmount.text doubleValue];

Remember the above code? Its to the equivalent of double d = Convert.ToDouble(tbx.Text) in C#. Over here, we are calling the doubleValue method of the String (or rather NSString - NS = NextStep) class to convert the text box Text property into a double.

NSNumberFormatter *numberFormatter = [[NSNumberFormatter allocinit];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
We now create the NumberFormatter object. Again its prefixxed with NS - NextStep. This object will format our 1.234566 into a nice 1.23 format. Of course you can format to percentages as well. There are also other formatters like date formatters.

NSNumberFormatter *numberFormatter = [[NSNumberFormatter allocinit];

We create the NumberFormatter object here. In short, we are doing a NumberFormatter nf = new NumberFormatter() here. [NSNumberFormatter alloc] is for allocating memory to hold this object. init - initialization. You will see [[Object alloc] init] very frequently.

NSNumber *n1 = [NSNumber numberWithDouble:paymentAmt];

Next, convert the double into a NSNumber type with the above code. Again, in C#, it will be NSNumber n1 = NSNumber.numberWithDouble(payment); Yea, its a static method.



 NSString *paymentAmtString = [numberFormatter stringFromNumber:n1];
Finally, using the numberFormatter object, which takes in a NSNumber argument, we finally convert our double to a String. Which we can assign to a text box Text property as per below.

monthlyPayment.text = paymentAmtString;

So in summary, double -> NSNumber -> NSNumberFormatter -> NSString.

Unbelieveable right, the amount of work you need to convert a formatted double into a String.
Yea, still more unbelievable things to come.
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment