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.
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+ (?=.)'
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 ...
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:
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.
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
You can use type conversion to decimal
$Test = "00000.22"
[decimal]$Test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With