Solved

Formatting with Powershell

Posted on 2016-09-14
9
44 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 5

Expert Comment

by:D Patel
Comment Utility
Define culture object as below and then try :

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

Author Comment

by:rdkli
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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
 

Author Comment

by:rdkli
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 82

Accepted Solution

by:
oBdA earned 400 total points (awarded by participants)
Comment Utility
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 68

Assisted Solution

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

Author Comment

by:rdkli
Comment Utility
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
Comment Utility
See Post above
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now