Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zeros in powershell

I have several strings and try to remove the leading zeros for them. I know I can use TrimStart() but it's wrong if all numbers are zero.

$Test = "00000.22"
$Test = $Test.trimstart('0')

The result is .22 and what I expect is 0.22. How can I achieve it? Thank you.

like image 682
Iverson Wang Avatar asked Mar 05 '18 19:03

Iverson Wang


People also ask

How to remove all leading zeros ( ^0+) from a string?

Positive look-ahead assertion (?= [^.]) effectively removes all leading zeros ( ^0+) as long as such a zero isn't followed by a literal . ( [^.] ). If you do want something like '00000.22' to become just '.22', i.e. if you want to drop the integral part if it happens to be 0, the regex becomes simpler: '^0+ (?=.)'

How do I add leading zeros to numbers in Excel?

Below are the steps to do this: 1 Select the cells that have the numbers with the leading zeros 2 Click the Home tab 3 In the Numbers group, click on the Number Format dropdown 4 Select ‘General’ More ...

How to drop all spaces before or after a string in PowerShell?

Is there an easy way to drop all spaces that are before or after a string in Windows PowerShell? Use the Trim () method to remove all spaces before and after a string from the output, for example:

Why are there leading zeros in a cell number?

If the number has been formatted as text (mostly by adding an apostrophe before the number), it would retain the leading zeros. The cell may have been formatted in such a way that it always shows a certain length of a number.


2 Answers

A purely textual solution is to use the -replace operator with a regular expression:

PS> '00000.22' -replace '^0+', '0' 
0.22

^0+ matches one or more (+) zeros at the start (^) of the string and replaces them with a single zero.

If you also want to make sure that numbers with an integral part that isn't 0 and/or without a fractional part are properly handled, use -replace '^0+(?=[^.])' (note the absence of a substitution operand, which effectively removes what gets matched):

PS> ('00000.22', '0000', '00001.22').ForEach({ $_ -replace '^0+(?=[^.])' })
0.22
0
1.22

Positive look-ahead assertion (?=[^.]) effectively removes all leading zeros (^0+) as long as such a zero isn't followed by a literal . ([^.]).

If you do want something like '00000.22' to become just '.22', i.e. if you want to drop the integral part if it happens to be 0, the regex becomes simpler: '^0+(?=.)'


gms0ulman's answer is convenient and probably works fine in most cases, but there are pitfalls:

  • You're changing the data type to [decimal], and converting that back to a string can result in a culture-specific representation where . is converted to , (that said, PowerShell itself often applies the invariant culture, which always uses .); e.g.:

      # Cast to [decimal] using the German culture ('de')
      PS> [System.Threading.Thread]::CurrentThread.CurrentCulture = 'de'; [decimal] '0000.22'
      0,22  # !! Note the "," instead of "."
    
  • Even though unlikely with a high-precision data type such as [decimal], rounding errors are possible:

      PS> [decimal] '00.99999999999999999999999999999'
      1.0000000000000000000000000000 # !! rounding occurred
    
like image 139
mklement0 Avatar answered Sep 20 '22 07:09

mklement0


You can use type conversion to decimal

$Test = "00000.22"
[decimal]$Test
like image 22
G42 Avatar answered Sep 18 '22 07:09

G42