swither
swither

Reputation: 13

Use a function Using-Culture in .TypeText in Powershell

I want to get the number like 1039,00 (comma is a decimal delimiter). But now i've got 1039.00

The thing is in the code:

$selection.TypeText("Angebotspreis: Using-Culture de-de {$TotalPrice} Euro ")

I've tried to do like this:

 $selection.TypeText("Angebotspreis: $(Using-Culture de-de {$TotalPrice}) Euro ")
 $selection.TypeText("Angebotspreis: @"Using-Culture de-de {$TotalPrice}"@ Euro ")

and so on.

Using-Culture function is:

 Function Using-Culture (
    [System.Globalization.CultureInfo]$culture = (throw “USAGE: Using-Culture -     Culture culture -Script {scriptblock}”),
    [ScriptBlock]$script= (throw “USAGE: Using-Culture -Culture culture -Script {scriptblock}”))
{
    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
    trap 
{
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
}
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
    Invoke-Command $script
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
}

also, in the console:

PS C:\Users\alexz> $(Using-Culture de-de {$TotalPrice})
7908.90

but

PS C:\Users\alexz> Using-Culture de-de {$TotalPrice}
7908,90

How can I fix it?

Upvotes: 1

Views: 356

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

Maybe you are looking for the overloaded ToString method on a double which you can pass a f for Floating point (decimal) values and a CultureInfo.

For example:

$ammount = 7908.39
$ammount.ToString('f', (New-Object System.Globalization.CultureInfo("de-DE")))

Output:

7908,39

Or if you want the sign and the dot for the thousends, use c for currency:

$ammount = 7908.39
$ammount.ToString('c', (New-Object System.Globalization.CultureInfo("de-DE")))

Output:

7.908,39 €

Upvotes: 1

Related Questions