0
> function h2n($num) {return [int]$num.replace(",", "")}

> h2n("1,341,096") | get-member



   TypeName: System.Int32

Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo(System.Object obj), int IComparable[int].Com... Equals      Method     bool Equals(System.Object obj), bool Equals(int obj), bool IEquatable[int].Equals(int other)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
ToBoolean   Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte      Method     byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar      Method     char IConvertible.ToChar(System.IFormatProvider provider)
ToDateTime  Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal   Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble    Method     double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16     Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32     Method     int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64     Method     long IConvertible.ToInt64(System.IFormatProvider provider)
ToSByte     Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle    Method     float IConvertible.ToSingle(System.IFormatProvider provider)
ToString    Method     string ToString(), string ToString(string format), string ToString(System.IFormatProvider provider), string ToString(string format, ... ToType      Method     System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16    Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32    Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64    Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)


> h2n("1,341,096")  * 0
1341096

Why does the last expression not return 0?

2
  • I changed to function h2n($nums) {$result = 0; $nums | % {$result = $result * 1000 + $_}; return $result}, still doesn't work Commented Mar 4, 2020 at 19:57
  • 1
    You are sending a string as parameter, not a number. The problem I now see is that you use brackets. In PowerShell you use spaces in calls to a function: h2n "1,341,096". If you want to multiply straight away, do (h2n "1,341,096") * 0 Commented Mar 4, 2020 at 20:03

1 Answer 1

1

You can enter [int](h2n('1,341,096')) * 0 when calling your function.

As the commenter above suggested you should enter (h2n '1,341,096') * 0.

The reason for this is because in Powershell, you don't enter function arguments in parentheses when calling the function on the shell. Instead, you use terminal-style syntax and separate the arguments with spaces and no parenthesis.

So what happens when you enter h2n('1,341,096') * 0 ? It performs this function call:

(h2n '1,341,096' * 0)

That just passed 3 parameters to your function, the first being 1,341,096, the second being *, and the third being 0. This is definitely not what you want.

That would be the functional equivalent of doing h2n('1,341,096', *, 0); in a normal programming language.

Instead, you invoke your function with a space like h2n '1,341,096' and then you surround it with parentheses so that you can use it in an inline expression.

Sign up to request clarification or add additional context in comments.

2 Comments

Since you are quoting my comment as answer, please correct it by removing the first line because that is wrong.
I included the first possibililty for completeness since it is possible to send arguments to a function surrounded by parentheses for special case syntax, e.g. ('ab','cd') would send a string array. So there are conceivable edge-cases where the original syntax might be used, and it IS possible to get the desired output with that syntax. But +1 for your suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.