gooly
gooly

Reputation: 1341

PowerShell formating numbers by variables

I have integer values: 3 60 150 1500 and float values 1.23354, 1.234, 1.234567...
I calculate the number of digits of the biggest integer:

    $nInt = [System.Math]::Ceiling([math]::log10($maxInt))
    # nInt = 4

and in another way the biggest number of dec. behind the decimal point of the float-variable: $nDec = 6

  1. How can I format a print out that all integer do have the same string-length with leading spaces?

    |1500
    | 500
    |  60
    |   3
    
  2. And all float with the same string-length as well?

       1.234567|
       1.23354 |
       1.234   |
    

The | is just to mark my 'point of measure'.

Of course I have to choose a character-set where all characters do have the same pixex-size.

I am thinking of formatting by "{0:n}" or $int.ToString(""), but I can't see how to use this.

Upvotes: 1

Views: 663

Answers (1)

Erik Blomgren
Erik Blomgren

Reputation: 886

Try PadLeft or PadRight. For example, for your integers:

$maxInt.ToString().PadLeft($nInt.ToString().Length, ' ')

Upvotes: 2

Related Questions