0

The output of a Powershell script I wrote in another country uses a comma for a decimal point instead of a period. So for example, instead of 30,43487 I want it 30.43487 or ,453 to be .453. Is there a simple way to do this in Powershell? If I convert every comma to a period using RegEx, I am worried it will affect big numbers such as making 653.999,99 -> 653.999.99. So wondering if there is a better way.

$divide= $num/5  
$divide
BeeFriedman
  • 1,800
  • 1
  • 11
  • 29
Bob_Dan
  • 3
  • 1
  • 4
  • No need for RegEx, unless you just want to. Replace or removing strings, values, text in data/files is a very common everyday use case, with many examples in the PowerShell Help files, blogs all over the web, MS docs, and videos on Youtube. So, yes, just read in the value and use the -Split and -Join on for comma with a period. There is an old adage. If you have to resort to RegEx, then you have two problems. Yout initial one, and trying to figure the RegEx thing. ;-} – postanote Nov 03 '20 at 04:53
  • 2
    I think you should read about culture specific formatting. https://stackoverflow.com/questions/2379514/powershell-formatting-values-in-another-culture – Doug Maurer Nov 03 '20 at 05:23

1 Answers1

1

You are not showing how you are reading or plan to read in these numbers. So, extending from my comment.

(30,43487) -split ',' -join '.'
# Results
<#
30.43487
#>

(,453) -split ',' -join '.'
# Results
<#
453
#>

'30,43487', ',453' | 
ForEach {($PSItem)  -split ',' -join '.'}
# Results
<#
30.43487
.453
#>

(@'
30,43487
,453
653.999,99
'@ | Split "`n`r") -split ',' -join '.'
# Results
<#
30.43487
.453
653.999.99
#>

There are a number of ways to do this. As you just could have used the -Replace as well.

postanote
  • 15,138
  • 2
  • 14
  • 25