在json数据中指定float的替代方法是什么? [英] what is the alternate to specify float in json data?

查看:99
本文介绍了在json数据中指定float的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JSON文件中指定具有浮点值的变量.但它似乎无法识别浮点值(例如 20.4444f,0.5f 等).所以我想知道有什么替代方法,以便将浮点值分配给JSON中的变量?

I need to specify variable having floating values in my JSON file. But it does not recognize float values (e.g. 20.4444f, 0.5f etc.), it seems. So I was wondering what could be the alternate to it in order to get float value assigned to variable in JSON?

此外,我将使用JSON文件在Java中设置大量数字参数(字符串,浮点型,双精度型).

Also, I will be using JSON file to set up lots of numeric parameter (of type string, float, double) in java.

  1. 当必须处理更多数字数据时,将使用哪种Java JSON API?非常感谢您的投入.

  1. Which will be appropriate java JSON API to use when one has to deal more with numeric data? Would really appreciate your inputs.

据我所知,Json Java API不支持从json文件读取浮点值.是对的吗 ?

As far as I know Json Java APIs does not support reading of floating values from json file. is that right ?

我可以想到一个粗略的解决方案,即将值指定为double,将其读取为Double,然后在实际使用它之前将其转换为float.但是我担心将double转换为float可能无法为我提供我在json文件中指定的确切值(如果可能,则为float).请输入您的意见!

I can think of one rough solution i.e. specifying value as double, reading it as Double and then converting it to float before actually using it. But I am afraid that converting double to float may not give me the exact value that I would have specified (as float if possible) in my json file. Your inputs please !

推荐答案

但是我担心将double转换为float可能无法为我提供我在json文件中指定的确切值(如果可能,则为float).请输入您的意见!

But I am afraid that converting double to float may not give me the exact value that I would have specified (as float if possible) in my json file. Your inputs please !

这很好:Java所使用的IEEE754格式保证 float -> double -> float 往返转换是准确的(唯一的例外是NaN值,该值可能不会保留有效载荷的位模式,但由于JSON始终不支持它们,因此这不会成为问题).

This is fine: the float -> double -> float roundtrip conversion is guaranteed to be exact by the IEEE754 format used by Java (the only exception is NaN values, which may not preserve payload bit patterns, but these won't be an issue as they're not supported by JSON anyway).

这篇关于在json数据中指定float的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆