Decimal
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
JSON 表示法 |
{
"value": string
} |
字段 |
value |
string
以字符串形式表示的小数值。 字符串表示形式由一个可选符号(+ [U+002B ] 或 - [U+002D ])、后跟零个或多个十进制数字(“整数”)、后跟一个可选的小数、后跟一个可选的指数组成。空字符串应解读为 0 。 小数部分由一个小数点后跟零个或多个十进制数字组成。字符串必须在整数或分数中包含至少一个数字。由符号、整数和分数组成的数字称为有效数。 指数由字符 e (U+0065 ) 或 E (U+0045 ) 后跟一个或多个十进制数字组成。 服务应在存储小数数值之前对其进行归一化处理,方法如下:
- 移除了明确提供的
+ 符号 (+2.5 -> 2.5 )。
- 将零长度的整数值替换为
0 (.5 -> 0.5 )。
- 强制将指数字符转换为大写,并带有明确的符号 (
2.5e8 -> 2.5E+8 )。
- 移除了明确提供的零指数(
2.5E0 -> 2.5 )。
服务可能会根据自身需求和所选的内部小数实现方式执行额外的归一化,例如同时移动小数点和指数值(示例:2.5E-1 <-> 0.25 )。此外,服务可能会保留小数部分末尾的零以指示更高的精度,但并非必须这样做。 请注意,仅支持使用 . 字符分隔整数和小数;无论使用何种语言区域,, 都不应受支持。此外,不应支持千位分隔符。如果某项服务支持这些值,则这些值必须进行标准化处理。 ENBF 语法如下:
DecimalString =
'' | [Sign] Significand [Exponent];
Sign = '+' | '-';
Significand =
Digits ['.'] [Digits] | [Digits] '.' Digits;
Exponent = ('e' | 'E') [Sign] Digits;
Digits = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' };
服务应清楚地记录支持的值范围、支持的最大精度(总位数)以及(如果适用)小数位数(小数点后的位数),以及在收到超出范围的值时的行为。 服务可以选择接受作为输入传递的值,即使该值的精度或小数位数高于服务支持的精度或小数位数,并且应该将该值四舍五入以适应支持的小数位数。或者,如果会损失精度,服务可能会返回 400 Bad Request (在 gRPC 中为 INVALID_ARGUMENT )错误。 如果服务收到的值超出支持的范围,则服务应返回 400 Bad Request 错误(在 gRPC 中为 INVALID_ARGUMENT )。
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eDecimal values are represented as strings in JSON, using a format similar to that of standard programming languages.\u003c/p\u003e\n"],["\u003cp\u003eThe decimal string representation allows for an optional sign, integer part, fractional part, and exponent.\u003c/p\u003e\n"],["\u003cp\u003eServices are encouraged to normalize decimal values before storage for consistency, for example by removing leading plus signs or zero-length integer values.\u003c/p\u003e\n"],["\u003cp\u003eWhile services should document their supported range, precision, and scale for decimal values, they may handle out-of-bounds values through rounding or error responses.\u003c/p\u003e\n"],["\u003cp\u003eThousand separators and the comma as a decimal separator are not supported in this representation and should be normalized if encountered by a service.\u003c/p\u003e\n"]]],[],null,["# Decimal\n\n- [JSON representation](#SCHEMA_REPRESENTATION)\n\nA representation of a decimal value, such as 2.5. Clients may convert values into language-native decimal formats, such as Java's [BigDecimal](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigDecimal.html) or Python's [decimal.Decimal](https://docs.python.org/3/library/decimal.html).\n\n| JSON representation |\n|-----------------------------|\n| ``` { \"value\": string } ``` |\n\n| Fields ||\n|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | `string` The decimal value, as a string. The string representation consists of an optional sign, `+` (`U+002B`) or `-` (`U+002D`), followed by a sequence of zero or more decimal digits (\"the integer\"), optionally followed by a fraction, optionally followed by an exponent. An empty string **should** be interpreted as `0`. The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in either the integer or the fraction. The number formed by the sign, the integer and the fraction is referred to as the significand. The exponent consists of the character `e` (`U+0065`) or `E` (`U+0045`) followed by one or more decimal digits. Services **should** normalize decimal values before storing them by: - Removing an explicitly-provided `+` sign (`+2.5` -\\\u003e `2.5`). - Replacing a zero-length integer value with `0` (`.5` -\\\u003e `0.5`). - Coercing the exponent character to upper-case, with explicit sign (`2.5e8` -\\\u003e `2.5E+8`). - Removing an explicitly-provided zero exponent (`2.5E0` -\\\u003e `2.5`). Services **may** perform additional normalization based on its own needs and the internal decimal implementation selected, such as shifting the decimal point and exponent value together (example: `2.5E-1` \\\u003c-\\\u003e `0.25`). Additionally, services **may** preserve trailing zeroes in the fraction to indicate increased precision, but are not required to do so. Note that only the `.` character is supported to divide the integer and the fraction; `,` **should not** be supported regardless of locale. Additionally, thousand separators **should not** be supported. If a service does support them, values **must** be normalized. The ENBF grammar is: DecimalString = '' | [Sign] Significand [Exponent]; Sign = '+' | '-'; Significand = Digits ['.'] [Digits] | [Digits] '.' Digits; Exponent = ('e' | 'E') [Sign] Digits; Digits = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' }; Services **should** clearly document the range of supported values, the maximum supported precision (total number of digits), and, if applicable, the scale (number of digits after the decimal point), as well as how it behaves when receiving out-of-bounds values. Services **may** choose to accept values passed as input even when the value has a higher precision or scale than the service supports, and **should** round the value to fit the supported scale. Alternatively, the service **may** error with `400 Bad Request` (`INVALID_ARGUMENT` in gRPC) if precision would be lost. Services **should** error with `400 Bad Request` (`INVALID_ARGUMENT` in gRPC) if the service receives a value outside of the supported range. |"]]