feat(math): add round options (#9117)

Add round options, but I think can also add floor, ceiling, etc. And
the default mode is trunc.

Closes #9117

Co-authored-by: Mahmoud Al-Qudsi <mqudsi@neosmart.net>
This commit is contained in:
Looouiiis
2024-05-30 12:15:02 +08:00
committed by Mahmoud Al-Qudsi
parent f1ae170155
commit 480d48351c
4 changed files with 101 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ Synopsis
.. synopsis::
math [(-s | --scale) N] [(-b | --base) BASE] EXPRESSION ...
math [(-s | --scale) N] [(-b | --base) BASE] [(-m | --scale-mode) MODE] EXPRESSION ...
Description
@@ -19,7 +19,6 @@ It supports simple operations such as addition, subtraction, and so on, as well
By default, the output shows up to 6 decimal places.
To change the number of decimal places, use the ``--scale`` option, including ``--scale=0`` for integer output.
Trailing zeroes will always be trimmed.
Keep in mind that parameter expansion happens before expressions are evaluated.
This can be very useful in order to perform calculations involving shell variables or the output of command substitutions, but it also means that parenthesis (``()``) and the asterisk (``*``) glob character have to be escaped or quoted.
@@ -37,8 +36,8 @@ The following options are available:
**-s** *N* or **--scale** *N*
Sets the scale of the result.
``N`` must be an integer or the word "max" for the maximum scale.
A scale of zero causes results to be truncated, not rounded. Any non-integer component is thrown away.
So ``3/2`` returns ``1`` rather than ``2`` which ``1.5`` would normally round to.
A scale of zero causes results to be truncated by default. Any non-integer component is thrown away.
So ``3/2`` returns ``1`` by default, rather than ``2`` which ``1.5`` would normally round to.
This is for compatibility with ``bc`` which was the basis for this command prior to fish 3.0.0.
Scale values greater than zero causes the result to be rounded using the usual rules to the specified number of decimal places.
@@ -49,6 +48,11 @@ The following options are available:
Hex numbers will be printed with a ``0x`` prefix.
Octal numbers will have a prefix of ``0`` but aren't understood by ``math`` as input.
**-m** *MODE* or **--scale-mode** *MODE*
Sets scale behavior.
The ``MODE`` can be ``truncate``, ``round``, ``floor``, ``ceiling``.
The default value of scale mode is ``round`` with non zero scale and ``truncate`` with zero scale.
**-h** or **--help**
Displays help about using this command.