kleros / kleros-juror-front

The front end for Kleros jurors.
MIT License
8 stars 5 forks source link

feat(bonding-curve): allow users to buy and sell PNKs #152

Closed yushih closed 6 years ago

yushih commented 6 years ago

@epiqueras Do I need to squash the commits?

epiqueras commented 6 years ago

Yeah that would be good.

epiqueras commented 6 years ago

Number.toFixed On Sat, Nov 24, 2018 at 4:00 PM yushih notifications@github.com wrote:

@yushih commented on this pull request.

In src/containers/tokens/components/bonding-curve-form/index.js https://github.com/kleros/kleros-juror-front/pull/152#discussion_r236044919 :

+} + +/**

    • Round number to 4 significant figures.
    • @param {string} n - Input number.
    • @returns {string} - Rounded number with at least 4 significant figures.
  • */ +function round(n) {
  • // Pad the number so it has at least 4 significant figures.
  • if (n.indexOf('.') === -1) n += '.'
  • n += '0000'
  • var count = 0
  • var dot = false
  • for (var i = 0; i < n.length; i++) {

What I need is to round the number so that it has 4 significant figures, not 4 decimal places.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kleros/kleros-juror-front/pull/152#discussion_r236044919, or mute the thread https://github.com/notifications/unsubscribe-auth/ASRQaGUDrr2yCyQAIlMxh_coDpBTzTJTks5uyV7vgaJpZM4YL8xp .

epiqueras commented 6 years ago

There are built-ins for this. Let’s not reinvent the wheel. On Sat, Nov 24, 2018 at 4:05 PM yushih notifications@github.com wrote:

@yushih commented on this pull request.

In src/containers/tokens/components/bonding-curve-form/index.js https://github.com/kleros/kleros-juror-front/pull/152#discussion_r236045056 :

  • if (n.indexOf('.') === -1) n += '.'
  • n += '0000'
  • var count = 0
  • var dot = false
  • for (var i = 0; i < n.length; i++) {
  • if (n[i] === '.') dot = true
  • else if (n[i] !== '0' || count > 0)
  • // Past leading zeros.
  • count += 1
  • if (count >= 4 && dot) break
  • }
  • // Remove trailing dot.
  • if (n[i] === '.') i -= 1

For example when the input is "12345" and n is "12345.0000". The loop terminates with i point to the decimal which we don't want to output.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kleros/kleros-juror-front/pull/152#discussion_r236045056, or mute the thread https://github.com/notifications/unsubscribe-auth/ASRQaAKzaXMh4lNzeEHGt5ru36lh94ZRks5uyWBKgaJpZM4YL8xp .

yushih commented 6 years ago

@epiqueras Number.toFixed won't do because it keeps some number of digits after the decimal points, but what I need is to have at least 4 significant digits. The closest is Number.toPrecision but it produces exponential notation, for example: (12345).toPrecision(4) becomes '1.235e+4'.

epiqueras commented 6 years ago

toFixed(4)?

On Sun, Nov 25, 2018 at 7:59 AM yushih notifications@github.com wrote:

@epiqueras https://github.com/epiqueras Number.toFixed won't do because it keeps some number of digits after the decimal points, but what I need is to have at least 4 significant digits. The closest is Number.toPrecision but it produces exponential notation, for example: (12345).toPrecision(4) becomes '1.235e+4'.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kleros/kleros-juror-front/pull/152#issuecomment-441419967, or mute the thread https://github.com/notifications/unsubscribe-auth/ASRQaIoFQZRcmitI_I5CQUq4ammsyWerks5uyj_ugaJpZM4YL8xp .

yushih commented 6 years ago

Say the original number is 0.00012345. (0.00012345).toFixed(4) evals to '0.0001', but I want '0.0001234'.