如何在PowerShell中正确舍入数字

Bya*_*aku 3 math powershell rounding

如标题所示,我在舍入数字时遇到一些问题。我的脚本当前如下所示:

[uint16]$Product1 = Read-Host "Enter the price of your product: "
...
#$TotalProducts contains the Value from all products together
Write-Host "You spent an amount of $TotalProducts for todays shopping."
Run Code Online (Sandbox Code Playgroud)

我希望程序将数字四舍五入,以使总数不是一个荒谬的长数字。它确实可以工作,但是在我手动计算之后,我看到程序计算了其他内容。

这里的问题是程序将例如122.50舍入122,而不是123

我尝试使用以下语法,但未成功:

[math]::Round($Product1)[System.Midpoint.Rounding]::AwayFromZero) = Read-Host "Enter the price of your product: "
Run Code Online (Sandbox Code Playgroud)

我是在尝试正确的方法,但只是在挑剔语法,还是这种方法完全错误?

Jon*_*sen 5

读主机返回一个字符串。

您应该先阅读,然后四舍五入。

$Product1 = Read-Host "Enter the price of your product: "
$RoundedNumber = [math]::Round($Product1, [System.MidpointRounding]::AwayFromZero)
Run Code Online (Sandbox Code Playgroud)

您现在拥有的舍入值$RoundedNumber