An open API service indexing awesome lists of open source software.

https://github.com/stcruy/to-exponential

Converts any big/small/precise decimal number represented as String, to exponential notation.
https://github.com/stcruy/to-exponential

decimal exponential notation number scientific toexponential

Last synced: about 1 year ago
JSON representation

Converts any big/small/precise decimal number represented as String, to exponential notation.

Awesome Lists containing this project

README

          

## to-exponential

Converts any arbitrarily big, small, or precise number represented as a String,
to exponential notation. For regular numbers it works just like JavaScript's
[`Number.toExponential()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential).

The returned String can be used as a unique identifier for the number's value.

### Specification

It accepts any decimal number represented as a String.

- It converts it to exponential notation without loss of precision.
- It works for high precision numbers, also for those that do not fit in a
64-bit JavaScript Number variable.
(They still have to fit in Strings, which have some maximum length).
- It works for very large and very small numbers, also for those with an
exponent (`e...`) that does not fit in a 64-bit JS number.

The exponent always gets a sign (+ or -), consistent with the output of
`Number.toExponential()`. So e.g. 5 becomes '5e+0' (not '5e0').

Returns `false` if the String does not represent a valid decimal number.

### Install

```
npm install to-exponential
```

### Examples

```
const toExponential = require('to-exponential');

console.dir( toExponential('0.5') ); // => '5e-1'.
console.dir( toExponential('5') ); // => '5e+0'.
console.dir( toExponential('50') ); // => '5e+1'.

console.dir( toExponential('10.5') ); // Outputs '1.05e+1'.
console.dir( toExponential('0.105e2') ); // Outputs '1.05e+1' too.

console.dir( toExponential(
'-12324.3434716349863831487632112E-561640934731461387461933142'
) );
// => '-1.23243434716349863831487632112e-561640934731461387461933138'.
```

### License

This project is licensed under the MIT license - see
[LICENSE.md](LICENSE.md).