Solved

Formatting with Powershell

Posted on 2016-09-14
9
58 Views
Last Modified: 2016-09-18
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
0
Comment
Question by:rdkli
9 Comments
 
LVL 7

Expert Comment

by:D Patel
ID: 41799269
Define culture object as below and then try :

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

Author Comment

by:rdkli
ID: 41799272
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?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41799294
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

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:rdkli
ID: 41799372
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
0
 
LVL 84

Accepted Solution

by:
oBdA earned 400 total points (awarded by participants)
ID: 41799438
Default thousands separator in en-US is a comma.
But you can of course change the CultureInfo to your likings, and use a section separator to handle the negative format.
$CultureInfo = New-Object System.Globalization.CultureInfo('en-US')
$CultureInfo.NumberFormat.NumberGroupSeparator = "'"
$Data = (New-Object PSObject @{InnerText = '1000000'}), (New-Object PSObject @{InnerText = '-1000000'})

$Data | % {
	$_.InnerText = ([int32]$_.InnerText).ToString("#,##0;(#,##0)", $CultureInfo)
}

$Data

Open in new window


Custom Numeric Format Strings
https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.80).aspx
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points (awarded by participants)
ID: 41799445
The apostrophe/tick as thousands separator is a swiss usus. Negative value representation in parens is originating from accounting.
0
 

Author Comment

by:rdkli
ID: 41799704
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 :)
0
 

Author Comment

by:rdkli
ID: 41799705
See Post above
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question