Avatar of rdkli
rdkli
Flag for Liechtenstein asked on

Formatting with Powershell

Hey there!

I'm currently working on a script which should prepare a xml-file for a import into a pdf-form.
Now I need to format some digits in the xml tags I defined.
The formatting part looks like this:
if( [int32]$_.innertext -lt 0) {$_.innertext = "(" + '{0;0,0}' -f [int32]$_.innertext + ")" } else { $_.innerText = '{0:0,0}' -f [int32]$_.innertex }

Open in new window


Now I'm aiming for this result:
If in the xml Tag the number is 1000000 -> it should make 1'000'000 out of it
if its -1000000 it should make (1'000'000)  out of it.

Well, I'm kinda pretty close to a solution for it - on my machine.. my machine-culture is currently  set to en-US, so i get 1'000'000 and (-1'000'000) with these formatting. but. The script need to run an a de-CH (other number formatting than en-US!) machine.

Does anyone have a clue how to do this? Can't I define a "better" integer formatting?
I have a feeling the culture-settings are to deep into this.

Best Regards
Powershell

Avatar of undefined
Last Comment
rdkli

8/22/2022 - Mon
D Patel

Define culture object as below and then try :

$a = New-Object System.Globalization.CultureInfo("de-CH")
rdkli

ASKER
Yeah, thx, but I don't get the numbers formatted as i want to.

Can't I format just the integer value as i want to? how does it work?
oBdA

So you want this to be able to be run on a de-CH machine, while still being formatted according to en-US?
Then get the CultureInfo you want and pass it to ToString():
$CultureInfo = New-Object System.Globalization.CultureInfo('en-US')
$Data = (New-Object PSObject @{InnerText = '1000000'}), (New-Object PSObject @{InnerText = '-1000000'})

$Data | % {
	$_.InnerText = If ([int32]$_.InnerText -lt 0) {
		'(' + ([int32]$_.InnerText).ToString('n0', $CultureInfo) + ')'
	} Else {
		([int32]$_.InnerText).ToString('n0', $CultureInfo)
	}
}

$Data

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
rdkli

ASKER
Thanks for your input!
Well, first of all: is formatting like 1'000'000 and minuses (1'000'000) really american?
Because your input doesn't work like wanted.

Is there no possibilty to define it like
{$_.innertext = "(" + '{000'000:0,0}' -f [int32]$_.innertext + ")" *-1 }else {$_.innertext = '{000'000:0,0}' -f $_.innerText}

Open in new window

that? mhh..

edited *-1 to it to eliminate the "-" in front of the number
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rdkli

ASKER
Well okay, so I need a Swiss separator for positiv numbers and a accounting thing for negatives.

And with oBdA's suggestion it works as i wanted! Thank you very much. And thx for the very informative link!

/problem solved, lesson learned :)
rdkli

ASKER
See Post above
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.