stdlib-js / stdlib-js/stdlib

[RFC]: add support for serializing a number to a string while avoiding exponential notation

Đang mở
#581 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
difficulty: 3 Feature JavaScript Needs Discussion priority: Normal RFC Utilities
Ngôn ngữ chính
JavaScript
Star
6k
Fork
1.3k
Merge trung bình
1 ngày 3 giờ
Pull request đã merge (30 ngày)
611

Mô tả

### Description

This RFC proposes serializing a number to string while avoiding exponential notation.

The default behavior for JavaScript runtimes when serializing either very large (>=10^21) or very small (<10^-6) numbers to a string is to convert them to exponential notation. E.g.,

```javascript
In [1]: (1 * base.pow(10,308)).toString()
Out[1]: '1e+308'

In [2]: (1 * base.pow(10,-323)).toString()
Out[2]: '1e-323'
```

However, in certain circumstances, you may want to avoid serializing to exponential notation and may prefer printing all digits (e.g., see [discussion](https://github.com/stdlib-js/stdlib/pull/563#discussion_r1006224348)).

For example, the following may be desirable behavior

```javascript
var str = serialize( 1e50 );
// returns '100000000000000000000000000000000000000000000000000'
```

This RFC proposes to add support for such behavior.

Package: `@stdlib/number/to-string` (?)
alias: `number2string`

### Related Issues

No.

### Questions

Built-in [`Number.prototype.toString( [radix] )`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) supports an optional `radix` argument. The proposed API would also support this argument, but also support an options object.

```javascript
/**
* Serializes a number to a string.
*
* @param {(PositiveInteger|Options)} [options] - radix or options
* @param {PositiveInteger} [options.radix] - radix
* @param {boolean} [options.exponential=true] - boolean indicating whether to serialize very large and very small values to exponential notation
* @throws {TypeError} must provide either a positive integer or an options object
* @throws {TypeError} must provide valid options
* @throws {RangeError} radix must be a positive integer on the interval [2, 36]
* @returns {string} serialized number
*/
function number2string( options ) {
// implementation...
}
```

Questions:

- Is overloading the existing built-in API desirable?
- Should serializing a number to a string in non-exponential format be a different package? If so, what would be that package's name?

### Other

Prior art:

- [SO](https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript)

### Checklist

- [X] I have read and understood the [Code of Conduct](https://github.com/stdlib-js/stdlib/blob/develop/CODE_OF_CONDUCT.md).
- [X] Searched for existing issues and pull requests.
- [X] The issue name begins with `RFC:`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.