-2

When I calculate with numbers in my App, there are no decimals in my output! this is the code I am using:

    int Vijfhonderdmeter = [_VijfHonderd.text intValue];
int vijftienhonderdmeter = [_vijftienhonderd.text intValue];

int vijfdeeldrie = vijftienhonderdmeter / 3;

int puntentotaal = Vijfhonderdmeter + vijfdeeldrie;
_PuntenTotaal.text = [NSString stringWithFormat:@"%d",puntentotaal];

The real output should be 87,18667, but my _Puntentotaal label only shows 87.

Has anyone got a solution?

By the way:

(_VijfHonderd.text intValue = 44.05).
(_vijftienhonderd.text intValue = 129.41).

Thank you for your time and help :)

san
  • 3,350
  • 1
  • 28
  • 40
Ruub020
  • 51
  • 2
  • 6
  • possible duplicate of [Why dividing two integers doesn't get a float?](http://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float) – Martin R Jan 10 '14 at 19:22

2 Answers2

2

3 problems:

1) You are using integer division with integer value. If you want floating point numbers you need to floating point values and do floating point division:

double Vijfhonderdmeter = [_VijfHonderd.text doubleValue];
double vijftienhonderdmeter = [_vijftienhonderd.text doubleValue];

double vijfdeeldrie = vijftienhonderdmeter / 3.0;

2) You are formatting the result using %d. Use %f with the double values.

3) Use NSNumberFormatter, not stringWithFormat: to format the numbers. This will ensure they look correct for all users based on their locale.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Your code will always return a non decimal value as you are operating with int datatype.

int Vijfhonderdmeter = [_VijfHonderd.text intValue]; here you are getting a non decimal value & later also you are operating it with other non-decimal values. To get decimal values, you should use `double` or `float` datatype.

For now,try this:

float Vijfhonderdmeter = [_VijfHonderd.text floatValue];
float vijftienhonderdmeter = [_vijftienhonderd.text floatValue];

float vijfdeeldrie = vijftienhonderdmeter / 3;

float puntentotaal = Vijfhonderdmeter + vijfdeeldrie;
_PuntenTotaal.text = [NSString stringWithFormat:@"%f",puntentotaal];

Refer this link for more knowledge about premitive datatypes in objective-C

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • That won't work either. Int divided by Int will still be int. Change it to `/ 3.0` or `/3.0f`. – Putz1103 Jan 10 '14 at 19:22
  • @Putz1103 No, the code is correct. `float / int` is done as `float`. – rmaddy Jan 10 '14 at 19:23
  • Ahh yes, my bad. I was still reading the op's int primitives, not the updates float primitives. – Putz1103 Jan 10 '14 at 19:24
  • I would, but you didn't explain at all why your code works and the op's does not. The correct code only helps this once. Understanding the error and why it is wrong helps down the road. – Putz1103 Jan 10 '14 at 19:28
  • @Putz1103. Nice gesture. Respect ... _/\\_ ... Edited my answer. Have a look at it & let me know if still something can be added – Prince Agrawal Jan 10 '14 at 19:35