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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top