Solved

Formatting with Powershell

Posted on 2016-09-14
9
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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 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 70

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

622 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