# Percentage Help

Discussion in 'iOS Programming' started by loon3y, Dec 1, 2011.

1. ### loon3y macrumors 65816

Joined:
Oct 21, 2011
#1
I've finally got some idea of a code to right for my percentage formula.

basically the user is going to type in the amount for the product and the deposit % in the respective textfields and have the the actual charged amount appear in the charged textfield by hitting a submit button.

so far i got this:

Code:
```-(IBAction)SubmitB;

{
int depositPercent = [deposit.text doubleValue];
float amountMoney = [amount.text doubleValue];
float depositMoney = (depositPercent / 100);
float actualPercent = (depositMoney * amountMoney);
NSString *amountString = [[NSString alloc] initWithFormat:@" %f", actualPercent];
charge.text = amountString;
}

```

i know the double value part is wrong but I'm not sure how to write it without it.

but the result comes out 0.00000

and I'm only trying to show xxxxxx.xx. with the last two numbers after the decimal representing cents.

2. ### 1458279 Suspended

Joined:
May 1, 2010
Location:
California
#2
float depositMoney = (depositPercent / 100);

Is your percent being divided by 100 ?
Don't you multiply by 100?

I don't know the parameters on the %f, but can you specify the number right of the point?

3. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#3

well for example lets say the customer gets charged 400 dollars but they only need to pay 20% deposit for now, which is 80 dollars, I'm trying to get that amount after applying the deposit requirement to appear on the charged textfield.

because whats actually charged is 80 because I'm only charging 20% of 400

so for the decimal problem it would be written like this?:

Code:
```
NSString *amountString = [[NSString alloc] initWithFormat:@" %.2f", actualPercent];
charge.text = amountString;
```

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#4
I'd suggest setting a breakpoint at the beginning of your method and then stepping through line-by-line. As each line executes, examine the values of your variables. Are they what you expect or not? This should help you narrow down where your problem is.

5. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Brobdingnag
#5
You need to understand what a type is. Specifically, what the different numeric types are. More specifically, what the int type is, and how that differs from the float and double types. Because this code is nonsense:
Code:
```    int depositPercent = [deposit.text doubleValue];
```
If you don't understand types, then you should at least do as dejo suggested and step through your code, examining the values at each step. This will show you where it goes wrong. To understand why it goes wrong, you will need to study up on the numeric types.

6. Dec 1, 2011
Last edited: Dec 1, 2011

### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#6
yea i knew it didn't make sense, but it wouldn't let me just put

Code:
```
int depositPercent = [deposit.text];
```

so i just changed it to the float, and it works now!

lol im so happy

edit: now i just need to find out how i can do this by just pressing the return key after typing in the deposit text field instead of pressing the submit button

7. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Brobdingnag
#7
I urge you to study the numeric types, so you will understand why it works. Otherwise it's just cargo cult programming (no understanding, ritualized actions, misplaced expectations, miraculous results).

8. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#8

oh, i hate not understanding the concept or the term.

i plan to have a full understanding, but this has been such a pain i just took the shortcut and it worked.

i dont understand y the code can't be

Code:
```
int depositPercent = [deposit.text];

```

how come this doesn't work? if floats you put double value what would you put for an int?

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#9
You don't understand because you are rushing headlong into coding and not stepping back to learn the fundamentals. On one side of your assignment is an int, on the other side is an NSString. You think that should be fine?

P.S. You're brackets are useless in this case.

10. ### 1458279 Suspended

Joined:
May 1, 2010
Location:
California
#10
First off, data has types: Date, Integers, Strings, Arrays, Objects, etc...

You are trying to take a .text and put it into an int.

A text could be "Joe" "Joe" is not an int and can't be an int.
5 can be an int, but @"5" is not an int.

Also, after you get a handle on types, you'll need to understand problems with converting from one type to another. Longs to Ints loose some of their value because longs store numbers differently.

You'll also have to understand how to compare numbers of different types: 1 doesn't always equal 1.0

11. ### iHutch105 macrumors member

Joined:
Aug 8, 2011
#11
Dejo is absolutely right here - you're rushing into this.

Can't understate how important it is to understand data types in any form of programming. You'll do yourself a world of good to roll back a few steps.

I don't know if there's just a couple of badly named variables or your maths is off but I can see a couple of problems (along with the obvious truncation) in the above code.

12. ### RonC macrumors regular

Joined:
Oct 18, 2007
Location:
Chicago-area
#12
I have a couple of recommendations for you:

a) Read the documentation for NSString. Pay particular attention to various conversion methods (e.g., doubleValue).

b) Read the String Programming Guide section on Formatting String Objects (it's referenced in the NSString documentation). Pay particular attention to the modifiers that can go with %f, for example, which are well described in the IEEE printf specification (referenced by the String Programming guide). HINT: expect something like %0.2f to help you, and when you read the documentation you'll see why that's the case.

c) A final hint: because of typing and conversions, sometimes types don't do what you expect them to do (until your expectations are set appropriately). What do you think this does?
Code:
```int a = 0.5;
int b = 0.999999;
if (a == b) {
NSLog(@"Hey, they're equal - see (%d = %d)",a,b);
} else {
NSLog(@"Hey, they're different - see (%d = %d)",a,b);
}
```
Change int to float and run again.

13. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#13
i don't know? but i can get rid of my brackets?

I'm only typing in numbers in the textfields since its a transaction part of the app with linea pro.

theres total amount, deposit percentage, and actual amount charge (what ever the deposit is)

so initially i think its probably good that i used float instead of int, because users might charge \$23.64, or they might want to get 25.5% deposit.

yea it was kinda badly named variables i just wanted to get it over with, I'm sure theres an easier way to code it in one line or something. but the math is correct, i tested it out.

it wouldn't work because its labeled as int but the number has a decimal.

yea I'm gonna read the nsstring documentation

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#14
You don't know if you don't know? How confused are you?

Just kidding. If you don't know, here's an opportunity to learn some more and find out why. Then you will know. Data types are a key concept of programming and converting between different types is a common task. Take the time to go learn about these things.

Yes, but you should understand why before you do. So, what purpose do the brackets normally serve? What is their key function as part of Objective-C programming?

15. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#15

yea I'm reading the documentation on nsstring, i read about different data types in the big nerd ranch book objective c, but i still didn't get it fully, but this is the 2nd time going through the iOS essentials book and its make a lot more sense to me since the 1st time around (which was my first book).

are you talking about the square brackets? the square brackets I'm not too sure,

but the curly backers i know u need that if u have more than one method or condition, and u need it to execute both otherwise than it'll just execute the first line only.

16. ### mobilehaathi macrumors G3

Joined:
Aug 19, 2008
Location:
The Anthropocene
#16
It would definitely work; that code snippet would compile and run. The question is what would you expect the output to be?

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#17
Yes, I'm talking about the square brackets. These: [ ]. And if you don't understand their purpose in Objective-C programming, which is a core concept by the way, and you've read all those books already, I'm not sure I can be of any further help. Sorry. Good luck, though.

18. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#18

i haven't finished the big nerd ranch objective c, i only got up to the 8th chapter.

thanks a lot for your help though

19. ### mobilehaathi macrumors G3

Joined:
Aug 19, 2008
Location:
The Anthropocene
#19
You shouldn't be at chapter 8 if you don't know what the square brackets do.

20. ### 1458279 Suspended

Joined:
May 1, 2010
Location:
California
#20
I'm going to give this one more shot.

Do not focus on what chapter your on, do not focus on what or how many books you've read.

Do NOT copy code and run it.

Learn a very simple program. Hand code it, do NOT cut and paste it from a book!

like what Ron wrote:
Then work from there, change things around an figure out how they work.

Programming is not an overnite deal, some of us have spend years doing this. I'm not saying it'll take years, but you'll burn yourself out if you don't stay in the same chapter until you understand it.

Might want to jump over to the ObjectC for dummies or C for dummies. Not trying to insult, but those books tend to have great explanations of concepts.

21. ### loon3y thread starter macrumors 65816

Joined:
Oct 21, 2011
#21
thanks for the advice everyone, it really helps a lot, its pretty dope, topics i asked bout months ago, i go back, and i can just figure it out by myself.

i just want everything to gel in already, yea I'm gonna take much more time with the chapters and tinker with it.

is objective C for dummies good? i remember people told me not to get dummy books (it was for something other than programming). but ill check it out.

i really like the iOS essentials books by neil smyth, I'm looking into getting the objective c 2.0 essentials book since i like his samples, its really on hand and you do a lot of coding and projects.

i never "copied and pasted" per say but i did write the coding from the book almost the same. only a few projects i tinkered and messed around with after i got deeper into the iOS essentials book.