Skip to content

[PSCustomObject] How to format the output of non-integer numbers? #19107

@237dmitry

Description

@237dmitry

Prerequisites

Steps to reproduce

I already opened a similar issue, then I did not receive a reasonable answer, I decided to wait until the new release, but nothing has changed.

If you use the table format, then trailing zeros are added to the non-integer numeric value:

[pscustomobject] @{ dollar = 1.95; monat = 20.01 }

dollar  monat
------  -----
 1,950 20,010

If I cast these values to a string type, then I will have to format the alignment in the columns of the table:

[pscustomobject] @{ dollar = 1.95; monat = 20.01 }
| Format-Table @{lab='dollar'; exp={$_.dollar.ToString()}; a='right'},
               @{lab='monat'; exp={$_.monat.ToString()}; a='right'}


dollar monat
------ -----
  1,95 20,01

or

[pscustomobject] @{ dollar = [string] 1.95; monat = [string] 20.01 }
| Format-Table @{lab='dollar'; exp={$_.dollar}; a='right'},
               @{lab='monat'; exp={$_.monat}; a='right'}

Or get this object as a list.

But the situation is such that the script outputs an object with more than thirty values and six properties.
Thus, the complete list will be 30 * 6 + 30 separators = 210 lines. The only possible option is a table.
But I will not format the output of the script directly in the console.

The variant with alignment right during the formation of the object is also not suitable, because there will be indents when viewed as a list.

[pscustomobject] @{ dollar = '{0,6}' -f 1.95; monat = '{0,5}' -f 20.01; weather = 'clear' }
| Format-List


dollar  :   1,95
monat   : 20,01
weather : clear

Advise what to do, in Windows it is tolerable, there is only one zero added, up to hundredths, and in Linux two zeros, up to thousandths.

Expected behavior

Table view of PsCustomObject should not add trailing zeroes, should return value of non-integer numeric as is.

Actual behavior

PSCustomObject adds trailing zeroes to table format.

Error details

No Errors

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.1
PSEdition                      Core
GitCommitId                    7.4.0-preview.1
OS                             Linux 6.1.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 01 Feb 2023 17:07:39 +0000
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

Horror

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions