My simple RubyMotion code:
data = DataParser.parse(url)
error_ptr = Pointer.new(:object)
json = NSJSONSerialization.JSONObjectWithData(data, options: 0, error: error_ptr)
The url is a weather API that provides a JSON with temperatures as floats like { "temp_c":22.4, ... }
. Strangely exactly that float 22.4
gets converted to 22.3999938964844
.
If I inspect data.to_s
the temperature reads 22.4
, so I assume the error lies within NSJSONSerialization.JSONObjectWithData
.
I wouldn't like to force rounding of all floats.
The decimal number "22.4" cannot be represented exactly by a binary floating point number such as float
or double
. So "rounding a float to 22.4" does not make sense, because there is no float that is exactly equal to 22.4.
You can only specify a precision if you convert the floating point number to a decimal string for output. You can use a NSNumberFormatter
or a printf-format like "%.<precision>f
.