JSON 标准 - 浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19554972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:57:18  来源:igfitidea点击:

JSON standard - floating point numbers

jsonfloating-pointstandardsexponent

提问by ThePiachu

I am wondering whether the following floating point notation is a valid JSON notation:

我想知道以下浮点表示法是否是有效的 JSON 表示法:

"result":{"base_fee":1e-005}

or should the exponent notation be replaced with a decimal notation?

还是应该用十进制表示法替换指数表示法?

回答by Delan Azabani

It is valid according to the format available at json.orgas numbers can optionally have a base 10 exponent denoted by an E, uppercase or lowercase, an optional plus or minus, and one or more digits.

根据json.org 上可用的格式,它是有效的,因为数字可以选择以 10 为底的指数,由 E、大写或小写字母、可选的加号或减号以及一位或多位数字表示。

image of JSON number format

JSON 数字格式的图像

回答by Qantas 94 Heavy

It's perfectly valid, according to RFC 4627RFC 7159*:

这是完全有效的,根据 RFC 4627RFC 7159*:

The representation of numbers is similar to that used in most programming languages. A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.

Octal and hex forms are not allowed. Leading zeros are not allowed.

A fraction part is a decimal point followed by one or more digits.

An exponent part begins with the letter E in upper or lowercase, which may be followed by a plus or minus sign. The E and optional sign are followed by one or more digits.

Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.

数字的表示类似于大多数编程语言中使用的表示。一个数字包含一个整数部分,它可以以一个可选的减号为前缀,后面可以跟一个小数部分和/或指数部分。

不允许使用八进制和十六进制形式。不允许有前导零。

小数部分是一个小数点后跟一位或多位数字。

指数部分以大写或小写字母 E 开头,后面可以跟一个加号或减号。E 和可选符号后跟一位或多位数字。

不允许使用不能表示为数字序列(例如 Infinity 和 NaN)的数值。

Exponents are permitted to have leading 0s, but not the integer section:

允许指数有前导 0,但不能有整数部分:

number = [ minus ] int [ frac ] [ exp ]

decimal-point = %x2E       ; .

digit1-9 = %x31-39         ; 1-9

e = %x65 / %x45            ; e E

exp = e [ minus / plus ] 1*DIGIT

frac = decimal-point 1*DIGIT

int = zero / ( digit1-9 *DIGIT )

minus = %x2D               ; -

plus = %x2B                ; +

zero = %x30                ; 0

* The RFC 7159 standard supercedes the RFC 4627 informational memo, however the grammar used remains exactly the same.

* RFC 7159 标准取代了 RFC 4627 信息备忘录,但使用的语法保持完全相同。

回答by mrtnhfmnn

While from a JSON (and JavaScript) perspective these four numerals

而从 JSON(和 JavaScript)的角度来看,这四个数字

a) 100
b) 100.0
c) 1.0E+2
d) 1E+2

a) 100
b) 100.0
c) 1.0E+2
d)1E+2

are just four ways to write the exact same number, in environments where integers and reals are distinct types of numbers they might not all be equivalent.

只是四种写出完全相同数字的方法,在整数和实数是不同类型的数字的环境中,它们可能并不都是等价的。

And while (a) clearly means an integer, and (b) a real, and (c) a real as well, the case (d) is a bit ambiguous: for example, in C this is a floating point literal(because there's an exponent), but in Ada it is an integer literal(because there's no decimal point).

虽然 (a) 明确表示整数,(b) 实数,(c) 实数,但情况 (d) 有点模棱两可:例如,在 C 中,这是一个浮点文字(因为有指数),但在 Ada 中它是一个整数文字(因为没有小数点)。

And in ISO 6093:1985"Information processing – Representation of numerical values in character strings for information interchange", the last one is invalid, while the other three correspond to the three distinguishable formats NR1, NR2, and NR3 defined there.

而在ISO 6093:1985《信息处理——用于信息交换的字符串中数值的表示》中,最后一个无效,而其他三个对应于那里定义的三种可区分格式NR1、NR2和NR3。

So in general—in JSON or elsewhere—, I would prefer and recommend to always include a decimal point in a "scientific" decimal string representation with an exponent.

因此,总的来说——在 JSON 或其他地方——我更喜欢并建议始终在带有指数的“科学”十进制字符串表示中包含小数点。

And to place at least one digit in front of the decimal point (if there is one), as JSON (and Ada, but not C) requires and ISO 6093 recommends (but not requires).

并在小数点前至少放置一位数字(如果有),正如 JSON(和 Ada,但不是 C)要求和 ISO 6093 建议(但不是要求)。

Just to avoid misunderstandings (among humans) or data exchange hassles (among machines and programs).

只是为了避免误解(人类之间)或数据交换麻烦(机器和程序之间)。