Giter Club home page Giter Club logo

Comments (9)

RobbieTheWagner avatar RobbieTheWagner commented on June 9, 2024

@Techn1x I could see that being useful. It's not supported by Math.round though.

from ember-math-helpers.

skeate avatar skeate commented on June 9, 2024

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.

RobbieTheWagner avatar RobbieTheWagner commented on June 9, 2024

@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.

Techn1x avatar Techn1x commented on June 9, 2024

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.

Techn1x avatar Techn1x commented on June 9, 2024

Hopefully some combination of Math.round(), Number.toFixed() and my above code snippet will produce a correctly rounded result...

from ember-math-helpers.

skeate avatar skeate commented on June 9, 2024

8.449 should round to 8.4. Rounding does not take into account anything past the number after the cutoff.

from ember-math-helpers.

RobbieTheWagner avatar RobbieTheWagner commented on June 9, 2024

@Techn1x @skeate either of you guys interested in submitting a PR for this?

from ember-math-helpers.

Techn1x avatar Techn1x commented on June 9, 2024

@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.

skeate avatar skeate commented on June 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.