1

I need to create a String from double the use String.Trim() to remove the full stop, but it doesn't remove it. I think there is also a way to do this numerically but I'd like to do it with the string. Is there a reason it won't remove it? The output from the code is 5.5

$MyDouble = 5.5

[String]$MyDouble2 = $MyDouble

$MyDouble2.Trim(".")

$MyDouble2

5 Answers 5

4

String.Trim() only trims from the beginning and end of strings, so it has no effect in your command, because the . only occurs inside your input string.

If you truly want to remove just the . and keep the post-decimal-point digits, use the -replace operator:

$MyDouble2 -replace '\.' # -> '55'

Note:
* -replace takes a regex (regular expression) as the search operand, hence the need to escape regex metacharacter . as \.
* The above is short for $MyDouble2 -replace '\.', ''. Since the replacement string is the empty string in this case, it can be omitted.

If you only want to extract the integer portion, use either 4c74356b41's .Split()-based answer, or adapt the regex passed to -replace to match everything from the . through the end of the string.

$MyDouble2 -replace '\..*' # -> '5'

@Matt mentions the following alternatives:

  • For removing the . only: Using String.Replace() to perform literal substring replacement (note how . therefore does not need \-escaping, as it did with -replace, and that specifying the replacement string is mandatory):

    $MyDouble2.Replace('.', '') # -> '55'
    
  • For removing the fractional part of the number (extracting the integer part only), using a numerical operation directly on $MyDouble (as opposed to via the string representation stored in $MyDouble2), via Math.Floor():

    [math]::Floor($MyDouble) # -> 5 (still a [double])
    
Sign up to request clarification or add additional context in comments.

Comments

3

Looking at some documentation for .Trim([char[]]) you will see that

Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

That does not cover the middle of strings, so using the .Replace() method would accomplish that.

I think there is also a way to do this numerically but I'd like to do it with the string.

Just wanted to mention that converting numbers to strings to then drop decimals via string manipulation is a poor approach. Assuming your example is what you are actually trying to do, I suggest using a static method from the [math] class instead.

$MyDouble = 5.5
[math]::Floor($MyDouble)

Comments

1
 $MyDouble = 5.5

 [String]$MyDouble2 = $MyDouble

 $MyDouble2.Replace(".", "")

Comments

0

Well, why would it trim not the last (or first) character? It wouldn't, what you need (probably) is:

$MyDouble = 5.5
[String]$MyDouble2 = $MyDouble
$MyDouble2.Split(".")[0]

Comments

0
$MyDouble = 5.5

[String]$MyDouble2 = $MyDouble

$res=$MyDouble2 -split "\."
$res[0..($res.Count-1)] -join ""

1 Comment

Answering twice, while not forbidden, is usually discouraged unless your approaches are drastically different. The case here is just a misunderstanding on how the trim method works. Also your code would be better off with a least a small explanation of what is happening.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.