5

How can i convert a string to float in Objective C without rounding it.?

I tried to convert a string 8.56021285234;

float result=[string floatValue];

giving 8.560213, the rounded value.

How can i avoid this? How i can get the exact value?

Nithin Michael
  • 2,166
  • 11
  • 32
  • 56

7 Answers7

4

You can't put more than about 7 digits of precision in a float. You need a type with more precision, like double, see here:

Difference between float and double

Community
  • 1
  • 1
Will
  • 805
  • 1
  • 9
  • 26
4

The problem is you're using a float. A float can usually only hold around 7-digits max. A double can hold many more digits. A float is 32-bit while a double is 64-bit therefore giving it "double" the precision. A simple work-around to your problem would be to do:

double result = [string doubleValue];

When logging, make sure to use NSLog(@"%.12f",result); to show the entire double as %f defaults to only a 6 decimal precision.

Milo
  • 5,041
  • 7
  • 33
  • 59
  • NSString *value = @"8.44654656565"; double dblValue = [value doubleValue]; NSLog(@"%f",dblValue); logging value 8.446547 – Nithin Michael Nov 29 '13 at 10:13
  • @Nitin Your error is in the formatting in your log. %f defaults to only to a 6 decimal precision. Use `NSLog(@"%.11f",dblValue);` to see the whole thing in the log. I have revised my answer. – Milo Nov 29 '13 at 10:16
  • :I want the string to convert in to exact float value. Because i need to use this later in the execution of my code. If i use %.11f i can log the value correctly. But still in my variable its only 6 decimal places – Nithin Michael Nov 29 '13 at 10:19
  • @Nitin The string is converting to an exact value. When I do the following code: `NSString *value = @"8.123456789";` `double dblValue = [value doubleValue];` `double newDbl = dblValue * 3;` `NSLog(@"%.9f", newDbl);` The log returns 24.370370367. That kind of decimal precision would only be possible if the string converted exactly. – Milo Nov 29 '13 at 10:22
  • Yea thanks man. I mistook that the result will be only 6 decimal places. Thanks for understanding me that. – Nithin Michael Nov 29 '13 at 10:27
3

Try double instead of float:

NSString *val = @"8.56021285234";
double num = [val doubleValue];
NSLog(@"Number ==> %f",num);
brandonscript
  • 68,675
  • 32
  • 163
  • 220
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
3

If you want to convert it use

 double result = [string doubleValue];

For displaying it use %.10f to specify decimal places:

NSLog(@"%.10f", result);
Greg
  • 25,317
  • 6
  • 53
  • 62
  • How do you know it's only 6 decimal places?Do you use %.10f when you display your result? Can you post your code? – Greg Nov 29 '13 at 10:01
  • NSString *value = @"8.44654656565"; double dblValue = [value doubleValue]; NSLog(@"double value %f",dblValue); Getting value 8.446547 – Nithin Michael Nov 29 '13 at 10:07
  • Change your NSLog to NSLog(@"double value %.10f",dblValue); and you will see that the value is not just 6 decimal places. %.10f means show 10 decimal places. – Greg Nov 29 '13 at 10:12
  • I want the string to convert in to exact float value. Because i need to use this later in the execution of my code. If i use %.10f i can log the value. But still in my variable its only 6 decimal places. – Nithin Michael Nov 29 '13 at 10:17
  • No in your variable dblValue is right value it's not rounded value. It's just your nsstring rounded it to display it in nicer way but it hasn't changed the value. – Greg Nov 29 '13 at 10:24
  • Yea thanks man. I mistook that the result will be only 6 decimal places. Thanks for understanding me that. – Nithin Michael Nov 29 '13 at 10:30
  • Also beware that with any floating point number there is always a limit to precision. You can never store the 'exact' value of a number with a float type beyond a certain number of places. To give a silly example, if you can find a way to store the exact value of PI in 64 bits you will get the Nobel prize. Be very careful with == and floats. It is not a good idea! – Will Nov 29 '13 at 10:35
1
 NSString *value = @"8.44654656565";
double dblValue = [value doubleValue];
NSLog(@"%.10f",dblValue);
Sabareesh
  • 3,585
  • 2
  • 24
  • 42
  • Humm.. Just copy my code and paste that code in to your application and then check it, when i checked here i am getting decimal value properly.. – Sabareesh Nov 29 '13 at 10:03
  • NSString *value = @"8.44654656565"; double dblValue = [value doubleValue]; NSLog(@"double value %f",dblValue); Getting value 8.446547 – Nithin Michael Nov 29 '13 at 10:06
  • NSString *value = @"8.44654656565"; double dblValue = [value doubleValue]; NSLog(@"%.12f",dblValue); Now check it – Sabareesh Nov 29 '13 at 10:09
  • I want to get the correct value in variable double dblValue. I want to use this value later in my code. – Nithin Michael Nov 29 '13 at 10:10
  • Yea thanks man. I mistook that the result will be only 6 decimal places. Thanks for understanding me that. – Nithin Michael Nov 29 '13 at 10:29
1

the double variable have the all information you need, then you format it as text system can display not the whole digits stored in double variable

The Sabareesh code:

NSString *value = @"8.44654656565";
double dblValue = [value doubleValue];
NSLog(@"%.12f",dblValue);

here dblValue have in memory all digits you want and you can use it as you want
NSLog here can prove you have enough data - simply because NSLog output it

Denis Kozhukhov
  • 1,205
  • 8
  • 16
0

Please try NSDecimal Number instead of [string floatValue];

NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"15.99"];
NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"29.99"];
NSDecimalNumber *coupon = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@".90"];
NSDecimalNumber *numProducts = [NSDecimalNumber decimalNumberWithString:@"2.0"];

NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber *averageSquared = [average decimalNumberByRaisingToPower:2];

NSLog(@"Subtotal: %@", subtotal);                    // 45.98
NSLog(@"After coupon: %@", afterCoupon);           // 40.98
NSLog((@"After discount: %@"), afterDiscount);       // 36.882
NSLog(@"Average price per product: %@", average);    // 18.441
NSLog(@"Average price squared: %@", averageSquared); // 340.070481
Indra
  • 528
  • 2
  • 11