How do I convert a string with spaces and commas to a double in PowerShell?

0
1
Asked By CuriousCoder83 On

Hey everyone! I'm running into a bit of a snag trying to convert strings to doubles in PowerShell. For example, I can successfully convert "82,85" without any issues, but when I try to convert "2 533,92", I get an error. It seems like the spaces or commas are messing it up. Is there a reliable way to make sure the conversion works correctly for different formats? Any tips would be greatly appreciated!

2 Answers

Answered By CodeNinja42 On

Yeah, the correct way is definitely to use the culture info like the other comment mentioned. But if you're unsure of the culture, you can try a workaround by removing all the spaces first:

```powershell
[double]::Parse($('2 533,92' -replace 's'))
``` That should do the trick!

Answered By TechGuru27 On

The main issue is the format of your string. You should specify the culture when parsing numbers so that PowerShell knows how to interpret the formatting. For example, you can use this:

```powershell
[double]::Parse("2 533,92", [cultureinfo]::new("fr-FR"))
```
If your PC is set to that culture, you can just use:

```powershell
[cultureinfo]::CurrentCulture
```

WittyUser99 -

It actually works perfectly! Thanks for the tip!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.