-2

I have

NSString *strAmount = @"10.00";
NSInteger totalAmount = totalAmount + [strAmount integerValue];

The output is

totalAmount = 80

but I want the output to be totalAmount = 10

Eric
  • 45
  • 1
  • 5
  • possible duplicate of [Objective C - Type Casting From NSString to Int](http://stackoverflow.com/questions/13046047/objective-c-type-casting-from-nsstring-to-int) – 7thFox Jul 07 '15 at 13:05
  • possible duplicate of [Convert NSString to NSInteger?](http://stackoverflow.com/questions/4791470/convert-nsstring-to-nsinteger) – Mayank Jain Jul 07 '15 at 13:12

3 Answers3

2
  1. First of all totalAmount should not be of pointer type.
  2. Variable 'totalAmount' is uninitialized when used within its own initialization

replace your code with

NSString *strAmount = @"10.00";
NSInteger totalAmount =  0;
totalAmount = totalAmount + [strAmount integerValue];
 NSLog(@" %ld",(long)totalAmount);

Output = 10

iAnurag
  • 9,286
  • 3
  • 31
  • 48
0

why you did pointer to int?

NSInteger totalAmount 0;
totalAmount = totalAmount + [strAmount integerValue];
Doro
  • 2,413
  • 2
  • 14
  • 26
0

Replace NSInterger *total by NSInteger total or convert NSInteger to int (C based)

NSString *strAmount = @"10.00";

NSInteger totalAmount = 0;
totalAmount = totalAmount + [strAmount integerValue];