On this page本页内容
$toDecimal
¶New in version 4.0.版本4.0中的新功能。
Converts a value to a decimal. If the value cannot be converted to a decimal, 将值转换为十进制。如果该值无法转换为十进制,则$toDecimal
errors. $toDecimal
发生错误。If the value is null or missing, 如果值为$toDecimal
returns null.null
或缺失,$toDecimal
返回null
。
$toDecimal
has the following syntax:语法如下所示:
The $toDecimal
takes any valid expression.$toDecimal
接受任何有效的表达式。
The $toDecimal
is a shorthand for the following $convert
expression:$toDecimal
是以下$convert
表达式的简写形式:
See also参阅
The following table lists the input types that can be converted to a decimal:下表列出了可以转换为十进制的输入类型:
Boolean | NumberDecimal("0") for false .false 返回NumberDecimal("0") 。NumberDecimal("1") for true .true 返回NumberDecimal("1") 。 |
Double | |
Decimal | |
Integer | |
Long | |
String |
|
Date |
The following table lists some conversion to decimal examples:下表列出了一些转换为十进制的示例:
{$toDecimal: true} |
NumberDecimal(“1”) |
{$toDecimal: false} |
NumberDecimal(“0”) |
{$toDecimal: 2.5} |
NumberDecimal(“2.50000000000000”) |
{$toDecimal: NumberInt(5)} |
NumberDecimal(“5”) |
{$toDecimal: NumberLong(10000)} |
NumberDecimal(“10000”) |
{$toDecimal: "-5.5"} |
NumberDecimal(“-5.5”) |
{$toDecimal: ISODate("2018-03-27T05:04:47.890Z")} |
NumberDecimal(“1522127087890”) |
Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
The following aggregation operation on the 在计算总价之前,orders
collection converts the price
to a decimal and the qty
to an integer before calculating the total price:orders
集合上的以下聚合操作将price
转换为十进制,将qty
转换为整数:
The operation returns the following documents:该操作返回以下文档: