Forestbird
Forestbird

Reputation: 31

Replace all commas in a array with dots

I have a Array with this output:

Names       TotalSizeInGB
-----       ----
User1       17,4
User2       9,81

Does anybody know how I can can make all the numbers with a comma (17,4 and 9,81) to nummbers with dots (17.4 and 9.81)

Upvotes: 0

Views: 1339

Answers (2)

Theo
Theo

Reputation: 61168

If you are referring to my previous answer, you can change the line

@{Name = 'TotalSizeInGB'; Expression = { '{0:F2}' -f (($_.Group | Measure-Object Length -Sum).Sum / 1Gb ) }}

to

@{Name = 'TotalSizeInGB'; Expression = { [double](($_.Group | Measure-Object Length -Sum).Sum / 1Gb ).ToString("F2", [cultureinfo]::InvariantCulture) }}

to get the numbers as numeric values (with a decimal point instead of a comma)

Otherwise, you can alter this afterwards and change the commas to get real numeric values from them using

foreach ($item in $array) {
    $item.TotalSizeInGB = [double]($item.TotalSizeInGB -replace ',', '.')
}

However, displaying this in your current locale (which uses a decimal comma, same as mine) will show they now are numbers (right-aligned), but automatically will display with a decimal comma:

Name  TotalSizeInGB
----  -------------
User1          17,4
User2          9,81

If you however export to CSV file, you'll see they have decimal points:

"Name","TotalSizeInGB"
"User1","17.4"
"User2","9.81"

If display in the console is all you're after, then simply leave out the cast to [double]. (this makes the values strings)


To clarify:

Use the first code for working with the numbers as numeric values (so having a dot as decimal point) in your script.
If you also want to display the table in the console where the numbers have decimal points, you need to replace the visual output, because if you don't, numbers are displayed with the machines [cultureinfo]::CurrentCulture.NumberFormat.NumberDecimalSeparator character, which in your case is the comma, not a dot.

For DISPLAYING the table with dots, you can do:

($Gesamt | Format-Table -AutoSize | Out-String) -replace ',', '.'

Again, this is for console output only. Nothing in the $Gesamt array is actually changed.

Upvotes: 2

deepakbhavale67
deepakbhavale67

Reputation: 347

for each iteration use the below line :

$string contains a line

$string = $string -replace '.', ''

Upvotes: 0

Related Questions