Comments (5)
Adding additional arguments to existing methods like those will not work because of existing use cases like array.map(Math.floor)
.
from proposal-math-extensions.
@zloirock, agree, then the best option would be to introduce another method, like Math.toFixed(value, decimals)
that would return a number value, naming can be different, not to be confused with Number.prototype.toFixed()
that returns a string.
from proposal-math-extensions.
FYI, similar library functions in https://github.com/stdlib-js/stdlib
- @stdlib/math/base/special/ceiln — Round a numeric value to the nearest multiple of 10^n toward positive infinity.
- @stdlib/math/base/special/floorn — Round a numeric value to the nearest multiple of 10^n toward negative infinity.
- @stdlib/math/base/special/roundn — Round a numeric value to the nearest multiple of 10^n.
- @stdlib/math/base/special/truncn — Round a numeric value to the nearest multiple of 10^n toward zero.
I also implemented a similar function in my library
/**
* Rounds a number to the nearest specified power of ten.
* @param {number} number - The number to round.
* @param {number} powerOfTen - An integer.
* @param {(function(number):number)=} mode - The rounding mode to use (default is `Math.round`).
* @return {number} The rounded number.
*/
function roundToDecimal(number, powerOfTen, mode) {
var FRACTION = 0;
var EXPONENT = 1;
/**
* A decimal arithmetic shift.
* @param {number} value - The number to shift.
* @param {number} offset - The exponent offset.
* @return {number} The shifted number.
*/
function shift(value, offset) {
/* This technique is credited to Lam Wei Li. */
var parts = String(value).split("e");
var exponent = Number(parts[EXPONENT] || "0") + offset;
return Number(parts[FRACTION] + "e" + exponent);
}
mode = mode || Math.round;
return shift(mode(shift(number, -powerOfTen)), powerOfTen);
}
roundToDecimal(3456.3456, -1, Math.ceil); // 3456.4
roundToDecimal(3456.3456, -2, Math.floor); // 3456.34
roundToDecimal(3456.3456, 1, Math.round); // 3460
roundToDecimal(3456.3456, 2, Math.trunc); // 3400
from proposal-math-extensions.
I would propose to add this parameter to Math.trunc()
instead.
from proposal-math-extensions.
That 2nd param reminds me of Python's round
. The only issue I see is that it assumes a constant base of 10, excluding all other bases, so a 3rd base
/radix
param should be added to make it future-proof, and more versatile in the present. A reason to support different bases is that both Number.prototype.toString
and parseInt
support it
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Methods to be added HOT 8
- [Suggestion] Math.mean and Math.roundBy HOT 2
- [Suggestion] Constants to be added HOT 1
- Is it still active? HOT 4
- [Suggestion] Math.lerp and Math.normalize HOT 2
- alternatives to Math.radians and Math.degrees HOT 5
- Math.exp2
- modulo function HOT 8
- Math.pow should accept Bigints, modulo and pow=-1 HOT 12
- Proposal Math.clamp separately HOT 1
- In the spec draft missed conversion of arguments to numbers
- Repository should be transferred to tc39 organization HOT 2
- Should `factorial` be defined in all or some domains? HOT 2
- Dot product?
- New rounding modes HOT 1
- Bitwise methods HOT 4
- Tuple methods
- `Number.prototype.toByteSize` / `Intl.SizeFormat` HOT 2
- `Math.copysign`? HOT 1
- `Math.MAX_RANDOM_BITS`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-math-extensions.