josdejong / josdejong/mathjs

`math.format()` and trailing zeros after decimal point in fixed notation

Open
#2,079 4 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
15.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

When `precision` is set to a value larger than 0, [`math.format()`](https://mathjs.org/docs/reference/functions/format.html) with the `fixed` notation appends trailing zeros after the decimal point when the value is an integer: https://github.com/josdejong/mathjs/blob/develop/src/utils/number.js#L353.

Would you be open to a new option to disable this behavior? I tried doing this with a custom format callback, but I quickly ended up copy-pasting hundreds of lines from the library code, because I also didn't want to lose the lots of great things `math.format()` is doing.

I would be happy to work on a PR to introduce a new option if you think it's a good idea.

What I'm after is exactly what the fixed notation gives me, but I would like to skip adding zeros to integers when I have the precision set to, say, 2, which will give me nicely rounded values after the decimal point. Currently the closest I can get to this is leaving the notation to use `auto` and set high enough `lowerExp` and `upperExp` values. This removes trailing zeros, but due to the different behavior of `precision` with this notation I can't do rounding only for decimal digits.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.