Search code examples
objective-cnsdictionarynsdecimalnumber

NSDictionary losing decimal place at retrieval


I have a json string something like this

"{"price":1.0}", using the json-framework I am converting this json string to NSDictionary.

Now I am retrieving the value from NSDictionary as below

NSString *text = [[dictionary objectForKey:@"price] description];

I am getting back only "1" as a result the decimal place and the 0 is gone.

But if i have a json string like this

"{"price":1.11}" then it just works fine, which means I get back 1.11 in my text.

I am displaying the price information on my app and it looks little wired to just display a number 1 :)

Does any one how do get back 1.0 instead of just 1?


Solution

  • The json string you are using does not represent a string, it represents a number so 1 and 1.0 are the same. For it to be a string it should be "{"price":"1.0"}". Though if all you care about is the display you can use [NSString stringWithFormat:@".2f",value]; to always return a value with 2 decimal places.