 # Decimal Functions and Operators

## Decimal Literals

Use `DECIMAL 'xxxxxxx.yyyyyyy'` syntax to define literal of DECIMAL type.

The precision of DECIMAL type for literal will be equal to number of digits in literal (including trailing and leading zeros). The scale will be equal to number of digits in fractional part (including trailing zeros).

Example literalData type
`DECIMAL '0'``DECIMAL(1)`
`DECIMAL '12345'``DECIMAL(5)`
`DECIMAL '0000012345.1234500000'``DECIMAL(20, 10)`

## Binary Arithmetic Decimal Operators

Standard mathematical operators are supported. The table below explains precision and scale calculation rules for result. Assuming `x` is of type `DECIMAL(xp, xs)` and `y` is of type `DECIMAL(yp, ys)`.

OperationResult type precisionResult type scale
`x + y` and `x - y``min(38, 1 + min(xs, ys) + min(xp - xs, yp - ys) )``max(xs, ys)`
`x * y``min(38, xp + yp)``xs + ys`
`x / y``min(38, xp + ys + max(0, ys-xs) )``max(xs, ys)`
`x % y``min(xp - xs, yp - ys) + max(xs, bs)``max(xs, ys)`

If the mathematical result of the operation is not exactly representable with the precision and scale of the result data type, then an exception condition is raised - `Value is out of range`.

When operating on decimal types with different scale and precision, the values are first coerced to a common super type. For types near the largest representable precision (38), this can result in Value is out of range errors when one of the operands doesn't fit in the common super type. For example, the common super type of decimal(38, 0) and decimal(38, 1) is decimal(38, 1), but certain values that fit in decimal(38, 0) cannot be represented as a decimal(38, 1).

## Comparison Operators

All standard comparison operators and `BETWEEN` operator work for `DECIMAL` type.

## Unary Decimal Operators

The `-` operator performs negation. The type of result is same as type of argument.

## 有奖捉虫

0/500

### 存在的问题

● 拼写，格式，无效链接等错误；

● 技术原理、功能、规格等描述和软件不一致，存在错误；

● 原理图、架构图等存在错误；

● 版本号不匹配：文档版本或内容描述和实际软件不一致；

● 对重要数据或系统存在风险的操作，缺少安全提示；

● 排版不美观，影响阅读；

● 描述存在歧义；

● 图形、表格、文字等晦涩难懂；

● 逻辑不清晰，该分类、分项、分步骤的没有给出；

● 很难通过搜索引擎，openLooKeng官网，相关博客找到所需内容；

● 命令、命令参数等错误；

● 命令无法执行或无法完成对应功能；

● 关键步骤错误或缺失，无法指导用户完成任务，比如安装、配置、部署等；

● 逻辑不清晰，该分类、分项、分步骤的没有给出

● 图形、表格、文字等晦涩难懂

● 缺少必要的前提条件、注意事项等；

● 描述存在歧义

0/500