question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatting system: format large numbers with thousands separators (digit grouping) by default

See original GitHub issue

Summary of the new feature/enhancement

The output formatting system’s purpose is to present data to the human reader. (As such - because the formatted output isn’t meant for programmatic processing - it isn’t part of PowerShell’s breaking-changes contract.)

Currently, large numbers are output without thousands separators, i.e. without grouping the digits for easier comprehension.

I suggest applying this grouping to all numbers (that aren’t explicitly formatted via formatting data):

# WISHFUL THINKING - but you can get a preview with the prototype below.

# Number by itself (out-of-band formatting)
PS> 1000
1,000

# Implicit in-band formatting (table or list)
PS> @{ num = 1000 }

Name                           Value
----                           -----
num                            1,000

# Should also automatically apply to types with explicit formatting data 
# (unless number formatting is part of a given list item / table column).
# Note the "Size" column.
PS> Get-Item $PROFILE

    Directory: /Users/jdoe/.config/powershell

UnixMode   User             Group                 LastWriteTime           Size  Name
--------   ----             -----                 -------------           ----  ----
-rw-r--r-- jdoe             staff              10/23/2019 22:31           1,934  Microsoft.PowerShell_profile.ps1

As @ThomasNieto proposes below, a new preference variable and new switch for the Format-* cmdlets could allow opting into the old behavior. E.g.: $PSThousandsGrouping with values $true and $false (default $true), and switch -ThousandsGrouping.

Proposed technical implementation details (optional)

Here’s a quick prototype that uses the ETS. It is not the suggested implementation, for reasons of both performance and also changing the behavior of explicit .ToString() calls.

A proper implementation would require modifying the formatting system itself.

# Prototype:
[int16], [int], [long], [double], [decimal], [bigint], [uint16], [uint], [uint64] | % {

  Update-TypeData -TypeName $_.FullName  -MemberType ScriptMethod -MemberName ToString -Value { 
    # Determine how many decimal places there are in the original representation.
    # Note: PowerShell's string interpolation uses the *invariant* culture, so '.'
    #       can reliably be assumed to be the decimal mark.
    $numDecimalPlaces = ("$this" -replace '^[^.]+(?:.(.+))?', '$1').Length

    # Format with thousands grouping and the same number of decimal places.
    # Note: This will create a culture-sensitive representation
    #       just like with the default output formatting.
     # CAVEAT:
     #  To avoid a crash (from infinite recursion?), both .psobject.BaseObject 
     #  and the -f operator must be used.
     #  ($this.psobject.BaseObject.ToString("...") also crashes).
    "{0:N$numDecimalPlaces}" -f $this.psobject.BaseObject
  } -Force

}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ThomasNietocommented, Sep 7, 2020

To get the current behavior there should be a switch parameter on the format commands and a preference variable to enable/disable globally.

1reaction
vexx32commented, Sep 7, 2020

I think that’s more likely to just be needlessly confusing, having one numeric type that behaves differently to the rest.

If this is going to change, all number types should behave similarly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Format numbers - Globalization
Format numeric values based on region standards using locale-aware methods, including decimal separators, grouping, and symbols.
Read more >
number formatting - How to set thousands separator in Java?
I have String representation of a BigDecimal that I want to format with a thousands separator and return as String. java · number-formatting...
Read more >
Show or hide the thousands separator
Select the cells that you want to format. On the Home tab, click the Dialog Box Launcher next to Number. Dialog Box Launcher...
Read more >
Decimal and Thousands Separators
The decimal separator is also called the radix character. Likewise, while the U.K. and U.S. use a comma to separate groups of thousands,...
Read more >
How to Apply Comma Style in Excel (Step by Step Tutorial)
The comma style in Excel is also known as the thousand separator format that converts the large number values with the commas inserted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found