Skip to content

Conversation

@initramfs
Copy link
Contributor

This PR adds the ability to specify an integer value for the pad parameter within the round dictionary in order to "only" pad to the specific number of decimal digits.

To demonstrate with an example:

`mode: "places"`
#ztable(
    columns: 5,
    format: (
        (round: (precision: 4, pad: false)),
        (round: (precision: 4, pad: true)),
        (round: (precision: 4, pad: 1)),
        (round: (precision: 4, pad: 3)),
        (round: (precision: 4, pad: 100)),
    ),
    table.header[`pad: false`][`pad: true`][`pad: 1`][`pad: 3`][`pad` #sym.gt.eq `precision`],
    ..([3],) * 5,
    ..([3.1],) * 5,
    ..([3.14],) * 5,
    ..([3.141],) * 5,
    ..([3.1415],) * 5,
    ..([3.14159],) * 5
)

`mode: "figures"`
#ztable(
    columns: 5,
    format: (
        (round: (mode: "figures", precision: 4, pad: false)),
        (round: (mode: "figures", precision: 4, pad: true)),
        (round: (mode: "figures", precision: 4, pad: 1)),
        (round: (mode: "figures", precision: 4, pad: 3)),
        (round: (mode: "figures", precision: 4, pad: 4)),
    ),
    table.header[`pad: false`][`pad: true`][`pad: 1`][`pad: 3`][`pad: 4`],
    ..([0.3],) * 5,
    ..([0.31],) * 5,
    ..([0.314],) * 5,
    ..([0.3141],) * 5,
    ..([0.31415],) * 5,
    ..([0.314159],) * 5
)

would yield:

tables-of-variable-rounding

The main purpose of this is essentially to specify a minimum bound on the number of decimal digits, useful in cases like always displaying a decimal point (for integers), or to display to a fixed number of fractional digits on average but allowing more precise figures to exceed the digit count (by setting precision high and pad to the expected number of decimal digits on average).


This change remains backwards-compatible with the existing specification of pad: true or pad: false as it merely adds an integer overload ontop of the existing behaviour.

@Mc-Zen
Copy link
Owner

Mc-Zen commented Dec 18, 2025

Hi @initramfs ,

thank you, this seems like a useful addition.

I think that in figures mode, it does not work correctly, though. I think that when the mode is set to "figures", the pad value should correspond to the number of significant figures. In the example you gave, it works by chance because there is no integer part but in general the count is wrong.

@initramfs
Copy link
Contributor Author

I had already implement sigfig-based padding as seen here (but scrapped it for the initial PR):

sigfig-based-rounding

Since this requires clarification of the semantics of pad (is it always digits or does it turn into sigfigs if mode: "figures").
This also incurs slightly extra complexity in round-or-pad() since we need to feed it the desired precision value as well.

I've added another commit that defines pad as being sigfig-based when in mode: "figures" since that seems to be the desired behaviour.

@Mc-Zen
Copy link
Owner

Mc-Zen commented Dec 18, 2025

Looks good now! Can you add a few tests to tests/rounding/test.typ in the style of the last tests in the file?

And maybe we should mention that pad has no influence when mode: "uncertainty".

@initramfs
Copy link
Contributor Author

Added tests and docs.

I amended the existing pad tests to expand on then rather than adding extra tests at the end, if that's okay with you.

@initramfs initramfs force-pushed the feat-variable-padding branch 2 times, most recently from 3a9019b to 3fd9c6e Compare December 18, 2025 17:51
@initramfs initramfs force-pushed the feat-variable-padding branch from 3fd9c6e to e2f06a2 Compare December 18, 2025 17:53
@Mc-Zen
Copy link
Owner

Mc-Zen commented Dec 18, 2025

Thank you! 🎉

@Mc-Zen Mc-Zen merged commit d3f8d48 into Mc-Zen:main Dec 18, 2025
1 check passed
@initramfs initramfs deleted the feat-variable-padding branch December 18, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants