> For the complete documentation index, see [llms.txt](https://documentation.soulver.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.soulver.app/zh/documentation/variables.md).

# 变量

### 声明变量

使用等号 `=` 声明一个变量。

```
discount = 10%                         | 10%
cost = $550                            | $550
cost - discount                        | $495.00  
```

变量可以是一个单词，也可以是一个完整的短语。

### 全局变量

在 **定义表** 中声明变量，使它们在所有工作表和 Soulver 文件中可用。就像使用常规变量一样使用 `=`。

在Mac上，打开**Settings > Variables**并单击**Edit Definition Sheet…**。

定义表中的全局变量也可以使用涉及其上方已声明变量的计算。

在Mac上，您还可以使用\*\*+**按钮直接在**Settings > Variables\*\*中添加全局变量。

### 变量优先级

如果同一个变量名在多处定义，Soulver 按以下顺序取值：

1. **局部变量** 在工作表中当前行上方声明。
2. **设置中的全局变量。**
3. **定义表中的变量。**

例如，如果定义表中设置了 `discount = 10%`，可以在某张表中使用 `discount = 20%`，为后续行覆盖该值，而不改变全局值。

### 重新定义变量

可以通过给变量赋予新值来重新定义它。

最近定义的值将在后续行中使用。

```
monthly rent = $1,900 // 2018      | $1,900
monthly rent = $2,150 // 2019      | $2,150   
monthly rent / 4 people            | $537.50
```

#### 变量可以被累加和减去

使用 `+=` 和 `-=` 运算符来修改变量的值

![](https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-8fa14eed7461f98993322520aaf02259d622d048%2Fimage%20\(2\)%20\(1\).png?alt=media)

## 使用变量的技巧

#### 查看变量的值

在 Mac 上，按住 Shift 键并悬停在变量上，即可查看其值。

<figure><img src="https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-9f2384537de4e905201ee4ed27a15c0173088d3f%2Fimage%20(5)%20(1).png?alt=media" alt="" width="158"><figcaption><p>在 Mac 上查看变量值</p></figcaption></figure>

在 iPad 和 iPhone 上，选择一个变量即可查看其值。

<figure><img src="https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-a765b7d87db5425280e152ad0512525656ae74d6%2Fimage%20(6)%20(1).png?alt=media" alt="" width="359"><figcaption><p>在 iPad 和 iPhone 上查看变量值</p></figcaption></figure>

#### 自动补全变量名

与其输入一个很长的变量名，不如按 **escape** 键，然后按 **return** 键，即可快速插入完整的变量名。

![按 escape 键自动补全长变量名](https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-ac96699d8bdd6e0b02227a52d0cccd21433ca5d5%2FScreen%20Recording%202019-06-04%20at%2004.09%20pm.gif?alt=media)

### 重命名变量

重命名变量时，Soulver 可以自动更新引用该变量的行。

在 Mac 上，将 **Settings > Variables** 中的 **Update lines when a variable is renamed** 设为 **Always**、**Ask** 或 **Never**。在 iPad 和 iPhone 上，可在 **Settings > Editing > Variable renaming** 中选择相同的选项。在变量声明所在行修改名称，然后将光标移到另一行。

## 关于变量声明需要注意的事项

#### 变量在使用前必须声明

您不能在变量被声明**之前**引用它。

此行为与编程语言中变量的工作方式一致（但与 Soulver 2 不同）。

#### 货币四舍五入不适用于变量声明行

当您以货币形式声明变量时，它将显示**不进行四舍五入**，这样可以明确变量中存储的实际值。

在后续行中使用时，货币四舍五入会像往常一样应用。

<figure><img src="https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-df66ca68c92b6c1dec43a9654a034d4ccb6437a4%2Fimage%20(12).png?alt=media" alt="" width="563"><figcaption><p>可以清楚地知道“每日成本”的实际值</p></figcaption></figure>

#### 变量声明可以排除在浮动总计之外

使用 “View > Total options” 菜单配置浮动总计是否应包含或排除变量声明行。

<figure><img src="https://2663624730-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO1ict55PUohNZzhtv5D%2Fuploads%2Fgit-blob-cd2f6be4c07a60accbaca965d8768fc73d7249d9%2Fimage%20(4)%20(1).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>
