一、Round() 函数语法
vbs
bash
Round(Expression[, NumDecimalPlaces])
参数说明:
表格
参数 类型 必需 说明
Expression Numeric ✅ 要取整的数值表达式
NumDecimalPlaces Integer ❌ 保留的小数位数(默认 0,即取整数)
二、使用示例
- 取整数(四舍五入)
bash
vbs
Dim result
result = Round(15.4) ' 结果:15
result = Round(15.5) ' 结果:16
result = Round(15.6) ' 结果:16
result = Round(-15.5) ' 结果:-16
- 保留小数位
bash
vbs
result = Round(15.678, 0) ' 15 (取整)
result = Round(15.678, 1) ' 15.7 (保留1位)
result = Round(15.678, 2) ' 15.68 (保留2位)
result = Round(15.674, 2) ' 15.67 (四舍五入)
- 应用到你的场景
bash
vbs
' 原始代码
scaledValue = (plcValue / 27648.0) * 20.0
' 取整数(方法1:默认参数)
scaledValue = Round((plcValue / 27648.0) * 20.0)
' 取整数(方法2:显式指定 0 位小数)
scaledValue = Round((plcValue / 27648.0) * 20.0, 0)
' 保留 1 位小数
scaledValue = Round((plcValue / 27648.0) * 20.0, 1)
三、完整 WinCC VBS 脚本
bash
vbs
'======================================================
' WinCC VBS - 使用 Round() 取整
'======================================================
Option Explicit
Dim objGauge ' 仪表盘对象
Dim plcValue ' PLC 原始值 (0~27648)
Dim mA_Value ' mA 值
Dim scaledValue ' 取整后的值
' ========== 1. 读取 PLC 变量 ==========
plcValue = HMIRuntime.Tags("QW404").Read
' ========== 2. 缩放到 0~20 mA ==========
mA_Value = (plcValue / 27648.0) * 20.0
' ========== 3. 四舍五入取整 ==========
scaledValue = Round(mA_Value)
' 或者写成一行:
' scaledValue = Round((plcValue / 27648.0) * 20.0)
' ========== 4. 更新仪表盘 ==========
Set objGauge = ScreenItems("圆形仪表")
objGauge.Value = scaledValue
' ========== 调试输出 ==========
HMIRuntime.Trace("PLC=" & plcValue & ", mA=" & mA_Value & ", 取整=" & scaledValue & vbCrLf)
四、Round() 的特殊行为(银行家舍入法)
bash
⚠️ 注意:VBS 的 Round() 使用 "银行家舍入"(Banker's Rounding)
vbs
' 当小数部分正好是 0.5 时,向最近的偶数取整
Round(0.5) ' 结果:0 (向偶数 0 舍入)
Round(1.5) ' 结果:2 (向偶数 2 舍入)
Round(2.5) ' 结果:2 (向偶数 2 舍入)
Round(3.5) ' 结果:4 (向偶数 4 舍入)
' 其他情况正常四舍五入
Round(1.4) ' 结果:1
Round(1.6) ' 结果:2
如果需要传统的 "四舍五入"(0.5 总是向上):
vbs
Function RoundUp(value)
RoundUp = Int(value + 0.5)
End Function
' 使用:
scaledValue = RoundUp((plcValue / 27648.0) * 20.0)
五、对比其他取整方法
| 方法 | 语法 | 15.4 | 15.5 | 15.6 | 说明 |
|---|---|---|---|---|---|
| Round() | Round(x) |
15 | 16* | 16 | 银行家舍入(0.5 看情况) |
| Int() | Int(x) |
15 | 15 | 15 | 向下取整(去掉小数) |
| Fix() | Fix(x) |
15 | 15 | 15 | 截断小数(负数行为不同) |
| CInt() | CInt(x) |
15 | 16 | 16 | 转换为 Integer(四舍五入) |
| 自定义 | Int(x + 0.5) |
15 | 16 | 16 | 传统四舍五入 |
六、实际应用建议
bash
场景 1:显示给操作员看(推荐 Round)
vbs
' 取整数
scaledValue = Round((plcValue / 27648.0) * 20.0)
' 或保留 1 位小数,更精确
scaledValue = Round((plcValue / 27648.0) * 20.0, 1)
场景 2:用于逻辑判断(推荐 Int 或 Fix)
vbs
' 向下取整,避免银行家舍入的不确定性
scaledValue = Int((plcValue / 27648.0) * 20.0)
' 用于判断阈值
If scaledValue >= 15 Then
' 执行操作
End If
场景 3:需要精确控制(自定义函数)
vbs
' 传统四舍五入(0.5 总是向上)
Function MyRound(value)
If value >= 0 Then
MyRound = Int(value + 0.5)
Else
MyRound = -Int(-value + 0.5)
End If
End Function
scaledValue = MyRound((plcValue / 27648.0) * 20.0)
七、在 WinCC 中插入函数
bash
方法 1:手动输入
vbs
scaledValue = Round((plcValue / 27648.0) * 20.0)
方法 2:使用函数选择器
markdown
1. 在脚本编辑器中右键 → "插入函数"
2. 找到 "Round" → 双击插入
3. 会自动生成:Round(Expression, NumDecimalPlaces)
4. 替换参数为实际值
八、完整测试脚本
bash
vbs
'======================================================
' 测试各种取整方法
'======================================================
Dim testValue
testValue = 15.678
HMIRuntime.Trace("原始值:" & testValue & vbCrLf)
HMIRuntime.Trace("Round(x):" & Round(testValue) & vbCrLf)
HMIRuntime.Trace("Round(x,1):" & Round(testValue, 1) & vbCrLf)
HMIRuntime.Trace("Round(x,2):" & Round(testValue, 2) & vbCrLf)
HMIRuntime.Trace("Int(x):" & Int(testValue) & vbCrLf)
HMIRuntime.Trace("Fix(x):" & Fix(testValue) & vbCrLf)
HMIRuntime.Trace("CInt(x):" & CInt(testValue) & vbCrLf)
HMIRuntime.Trace("Int(x+0.5):" & Int(testValue + 0.5) & vbCrLf)
' 测试 0.5 的情况
testValue = 15.5
HMIRuntime.Trace(vbCrLf & "测试 15.5:" & vbCrLf)
HMIRuntime.Trace("Round(15.5):" & Round(testValue) & vbCrLf)
HMIRuntime.Trace("Int(15.5+0.5):" & Int(testValue + 0.5) & vbCrLf)
九、最终推荐代码(单行版)
vbs
' 最简洁的写法(取整数)
objGauge.Value = Round((HMIRuntime.Tags("QW404").Read / 27648.0) * 20.0)
' 保留 1 位小数
objGauge.Value = Round((HMIRuntime.Tags("QW404").Read / 27648.0) * 20.0, 1)
总结:
✅ 取整数:Round(表达式) 或 Round(表达式, 0)
✅ 保留小数:Round(表达式, 小数位数)
⚠️ 注意 VBS 的 Round 使用银行家舍入(0.5 特殊处理)
🎯 工业显示一般用 Round() 就够了