Domanda

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_sthe temperature reads 22.4, so I assume the error lies within NSJSONSerialization.JSONObjectWithData.

  • Can anyone confirm this?
  • Is there a common solution for this?

I wouldn't like to force rounding of all floats.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top