PowerShell 数学错误

标签 powershell powershell-2.0

为什么当答案介于 0 和 1 之间时,PowerShell 有时似乎会给我错误的答案?

PS C:\users\me> 31.2 - 31
0.199999999999999   <<< wrong
PS C:\users\me> 31.2 - 30
1.2                 <<< right
PS C:\users\me>  31.2 % 10
1.2                 <<< right
PS C:\users\me> 30.2 % 10
0.199999999999999   <<< wrong
PS C:\users\me> 30.2 / 151
0.2                 <<< right

最佳答案

您需要使用 [decimal] 来计算街头行人。

[decimal]31.2 - [decimal]31

31.2d - 31

数字字面量默认为double,不能表示很多小数(因为需要重复分数,涉及舍入误差等)

引用: https://devcentral.f5.com/articles/powershell-abcs-t-is-for-type-literals

关于PowerShell 数学错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369723/

相关文章:

sorting - 在哈希表数组上使用排序对象

powershell - 创建 PowerShell 连接字符串

powershell foreach-object 与 if 语句

powershell - Powershell查找注册表项并将其删除

azure - 使用 Microsoft Graph Powershell 删除策略

mysql - 将AD用户的密码与mysql数据库同步

powershell - 作为版本 2 运行远程 Powershell session

powershell - 将内容输入文本文件的脚本

sharepoint - 从存储在文档库中的 Excel 中获取数据并使用 powershell 将其显示在列表中

batch-file - 错误 本地场不可访问。带有 FeatureDependencyId 的 Cmdlet 未注册