Comments (9)
@Techn1x I could see that being useful. It's not supported by Math.round
though.
from ember-math-helpers.
It is supported (kind of) by Number
, though. (2.1254).toFixed(2) === '2.13'
. Unfortunately (depending on use case), it also adds zeroes: (2).toFixed(2) === '2.00'
from ember-math-helpers.
@skeate should we have the round
helper use Math.round
unless you pass decimals
, and if you specify decimals use toFixed
?
from ember-math-helpers.
Also worth noting is that Number.toFixed()
returns a string result, also it doesn't take into account values further on, for example (8.449).toFixed(1) === "8.4"
whereas it should be 8.5
?
I wrote this helper few weeks ago to solve the issue for myself. However if you do something like 8.449
rounded to 1 decimal place, it returns 8.4
(whereas it should return 8.5
) so it's not entirely correct either, but if someone is clever enough to figure it out properly this might help;
import { helper } from '@ember/component/helper';
// Rounding decimal places since it doesn't seem to exist as a js math function, or as an ember-math-helpers helper
// NOTE: This will currently only work for decimals >= 0, with some work negative values could probably be supported
export function roundDecimal([value,decimals]) {
if(!value || decimals < 0) return value;
if(!decimals || decimals === 0) return Math.round(value);
var sign = '-';
return Number(Math.round(value + 'e' + decimals) + 'e' + sign + decimals);
}
export default helper(roundDecimal);
from ember-math-helpers.
Hopefully some combination of Math.round()
, Number.toFixed()
and my above code snippet will produce a correctly rounded result...
from ember-math-helpers.
8.449 should round to 8.4. Rounding does not take into account anything past the number after the cutoff.
from ember-math-helpers.
@Techn1x @skeate either of you guys interested in submitting a PR for this?
from ember-math-helpers.
@skeate oh haha my bad, you're right. Seems rounding is only supposed to look at the next number along and that's it. That being said, toFixed()
still doesn't seem to work right.
For example, here's some funky answers I got;
(8.45).toFixed(1) === '8.4'
(incorrect, should be 8.5)
(8.49).toFixed(1) === '8.5'
(correct)
(8.65).toFixed(1) === '8.7'
(correct)
(Just to be 100% sure I am now using this tool to double check my answers https://www.calculatorsoup.com/calculators/math/roundingnumbers.php )
@rwwagner90 I can probably write a PR for this this weekend, if no-one beats me to it
from ember-math-helpers.
That discrepancy is a result of the floating point representation of (e.g.) 8.45:
> 8.45 - 1
7.449999999999999
( - 1 because otherwise it'll just dutifully report "8.45")
There's a version that works here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding
from ember-math-helpers.
Related Issues (20)
- Support all Math methods HOT 1
- The `log` helper breaks in Ember 2.10 HOT 8
- Release doc for 2.x HOT 3
- An in-range update of ember-cli-sass is breaking the build 🚨
- An in-range update of ember-cli-eslint is breaking the build 🚨
- An in-range update of ember-cli-shims is breaking the build 🚨
- dependencies vs devDependencies HOT 4
- An in-range update of babel-eslint is breaking the build 🚨
- An in-range update of ember-cli-sass is breaking the build 🚨
- An in-range update of ember-source is breaking the build 🚨 HOT 2
- An in-range update of ember-cli-sass is breaking the build 🚨
- Test ticket from Code Climate
- Action required: Greenkeeper could not be activated 🚨
- Update breaks Ember builds for Ember 3.7 HOT 21
- only/except not working for app tree HOT 2
- docs is not working HOT 1
- Add types (for Glint) HOT 3
- Namespace '"@ember/component/helper"' has no exported member 'EmptyObject'. HOT 6
- Tree-shaking via only / except configuration is broken in v4 HOT 5
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 ember-math-helpers.