Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSJSONSerialization output number as float?

I'm using NSJSONSerialization to convert a dictionary into JSON.

If I include an NSDecimalNumber ( == 0 ) in that dictionary it outputs as 0. This is wrong. 0 is an int. I need it to output as 0.0.

This is what I'm doing:

NSDecimalNumber *decimal = [[NSDecimalNumber alloc] initWithFloat:0.0f];
// when fed into NSJSONSerialization it outputs as 0

Is there any way to output as 0.0?

OR am I incorrect in my assumption? Is 0 a valid float?

like image 915
Thomas Clayson Avatar asked May 14 '13 16:05

Thomas Clayson


1 Answers

There is no way to affect the way NSJSONSerialization outputs numbers. But you really should not worry about this. JSON doesn’t distinguish between different types of numbers, so you always should accept numbers with and without decimal points, no matter what the actual type of number you are doing your calculations with.

like image 164
Sven Avatar answered Sep 23 '22 01:09

Sven