Shibu
Shibu

Reputation: 1

Adding Decimal to an Integer

I need to add decimal to an integer. Eg: Amount = 12345

The output should be Amount = 123.45

Could someone help me how to achieve this using power shell

Upvotes: 0

Views: 2833

Answers (1)

colsw
colsw

Reputation: 3336

Always use a comma if you're looking to format a long string, adding a decimal point implies the number has a decimal component.

(12345).ToString("N0")
12,345

the N0 is a default formatting string which here gives the comma separated string.

if you're looking to fix badly stored decimal numbers or something where your question is actually what you're looking for, dividing by 100 will work for your needs.

12345 / 100
123.45

if you need a more code based solution which handles trailing zeroes or something you could use this:

$num = 12345
$numstr = "$num"
$splitat = $numstr.Length - 2
$before = $numstr.Substring(0,$SplitAt)
$after = $numstr.Substring($SplitAt)
"$($before).$($after)"

or this

"12345" -replace '(\d*)(\d{2})','$1.$2'

Upvotes: 2

Related Questions