Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting with Powershell

Posted on 2016-09-14
9
Medium Priority
?
84 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 85

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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

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 85

Accepted Solution

by:
oBdA earned 1600 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 71

Assisted Solution

by:Qlemo
Qlemo earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

916 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