gpt4 book ai didi

powershell - 使用Powershell提取小数点前的数字

转载 作者:行者123 更新时间:2023-12-03 00:28:04 24 4
gpt4 key购买 nike

我正在使用Powershell,需要提取小数点前的数字,以便评估提取的数字

所以用$Interest_Rate = 15.5
我尝试了以下代码..但是它们不起作用:

$Interest_RatePart1 = "{0:N0}" -f $Interest_Rate

将值舍入为16
$Interest_RatePart1 = ($Interest_Rate -split '.')[0].trim()

它返回一个空白。
我只想归还15

最佳答案

Formatting the number will cause rounding away from zero

使用 Math.Truncate() -总是四舍五入-代替:

$Interest_RatePart1 = [Math]::Truncate($Interest_Rate)

FWIW,您上一次尝试不返回任何内容的原因是,因为 -split默认为正则表达式,而 .表示正则表达式中的任何字符。

使用 .逃脱 \:
$Interest_RatePart1 = ($Interest_Rate -split '\.')[0].Trim()

或指定它不应该使用正则表达式:
$Interest_RatePart1 = ($Interest_Rate -split '.', 2, 'SimpleMatch')[0].Trim()

或改用 String.Split()方法:
$Interest_RatePart1 = "$Interest_Rate".Split('.')[0].Trim()

关于powershell - 使用Powershell提取小数点前的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48477756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com