string Powershell:将字符串转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25322636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:15:37  来源:igfitidea点击:

Powershell: convert string to number

stringpowershell

提问by MMAX

Ok, I am beginner - and lost with this:

好的,我是初学者 - 并因此迷失了:

I have an Array where some drive data from WMI are captured:

我有一个数组,其中捕获了来自 WMI 的一些驱动器数据:

$drivedata = $Drives | select  @{Name="Kapazit?t(GB)";Expression={$_.Kapazit?t}}

The Array has these values (2 drives):

阵列具有以下值(2 个驱动器):

@{Kapazit?t(GB)=1.500} @{Kapazit?t(GB)=1.500}

and just want to convert the 1.500 into a number 1500

并且只想将 1.500 转换为数字 1500

I tried different suggestions I found here, but couldn't get it working:

我尝试了在这里找到的不同建议,但无法使其正常工作:

-Replace ".","" and [int]doesn't work. I am not sure if regex would be correct and how to do this.

-Replace ".","" and [int]不起作用。我不确定正则表达式是否正确以及如何执行此操作。

For help many thanks in advance.

非常感谢您的帮助。

回答by Roderick Bant

Simply casting the string as an intwon't work reliably. You need to convert it to an int32. For this you can use the .NET convertclass and its ToInt32method. The method requires a string($strNum) as the main input, and the base number(10) for the number system to convert to. This is because you can not only convert to the decimal system (the 10base number), but also to, for example, the binary system (base 2).

简单地将字符串转换为 anint不会可靠地工作。您需要将其转换为int32. 为此,您可以使用 .NETconvert类及其ToInt32方法。该方法需要一个string$strNum)作为主要输入,以及base number10)作为数字系统转换为。这是因为您不仅可以转换为十进制系统(10基数),还可以转换为二进制系统(基数 2)。

Give this method a try:

试试这个方法:

[string]$strNum = "1.500"
[int]$intNum = [convert]::ToInt32($strNum, 10)

$intNum

回答by MMAX

Simply divide the Variable containing Numbers as a string by 1. PowerShell automatically convert the result to an integer.

只需将包含数字作为字符串的变量除以 1。PowerShell 会自动将结果转换为整数。

$a = 15; $b = 2; $a + $b --> 152

$a = 15; $b = 2; $a + $b --> 152

But if you divide it before:

但是如果你之前把它分开:

$a/1 + $b/1 --> 17

$a/1 + $b/1 --> 17

回答by cloud_hero

Since this topic never received a verified solution, I can offer a simple solution to the two issues I see you asked solutions for.

由于此主题从未收到过经过验证的解决方案,因此我可以为我看到您要求解决方案的两个问题提供一个简单的解决方案。

  1. Replacing the "." character when value is a string
  1. 替换“。” 值是字符串时的字符

The string class offers a replace method for the string object you want to update:

string 类为您要更新的字符串对象提供了一个替换方法:

Example:

例子:

$myString = $myString.replace(".","") 
  1. Converting the string value to an integer
  1. 将字符串值转换为整数

The system.int32 class (or simply [int] in powershell) has a method available called "TryParse" which will not only pass back a boolean indicating whether the string is an integer, but will also return the value of the integer into an existing variable by reference if it returns true.

system.int32 类(或在 powershell 中简称为 [int])有一个名为“TryParse”的可用方法,它不仅会传回一个指示字符串是否为整数的布尔值,还会将整数的值返回到现有的如果返回true,则引用变量。

Example:

例子:

[string]$convertedInt = "1500"
[int]$returnedInt = 0
[bool]$result = [int]::TryParse($convertedInt, [ref]$returnedInt)

I hope this addresses the issue you initially brought up in your question.

我希望这可以解决您最初在问题中提出的问题。

回答by Ray

It seems the issue is in "-f ($_.Partition.Size/1GB)}}" If you want the value in MB then change the 1GB to 1MB.

似乎问题出在“-f ($_.Partition.Size/1GB)}}”中。如果您想要以 MB 为单位的值,那么将 1GB 更改为 1MB。