I am looking for an easy way to convert numeric strings e.g. “1.78M” “1.47B” to an integer variable.
Any help is appreciated.
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Are you looking for
M==MBorM==1E6? If it is the former, PowerShell understands KB, MB, GB and TB e.g.:Big caveat here with
Invoke-Expression, if you’re getting the string from a user, file, i.e. an untrusted source. You have to be careful about executing it. Say the string is"2MB; Remove-Item C:\ -Recurse -Force -Whatif -EA 0", you’d have a bad day usingInvoke-Expressionon that string. BTW, I’m being nice here by adding the -Whatif. 🙂If it is the latter, you could do a regex -replace followed by a coercion e.g.: