Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top