5

I'm working on a function in JavaScript. I take two variables x and y.

I need to divide two variables and display result on the screen:

x=9; y=110;
x/y;

then I'm getting the result as :

0.08181818181818181

I need to do it with using some thing like BigDecimal.js that I found in another post.

I want that result was shown as:

0.081

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
  • possible duplicate of [Dealing with float precision in Javascript](http://stackoverflow.com/questions/11695618/dealing-with-float-precision-in-javascript) – Alex Kulinkovich Nov 21 '14 at 14:06
  • which in turn is duplicate for: https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – Scheintod Jun 09 '15 at 19:48

5 Answers5

20

Try this it is rounding to 3 numbers after coma:

(x/y).toFixed(3);

Now your result will be a string. If you need it to be float just do:

parseFloat((x/y).toFixed(3));
kajojeq
  • 886
  • 9
  • 27
  • 1
    Yes but note that `.toFixed()` returns a string, not a number. May not matter here of course. – Pointy Nov 14 '13 at 14:53
  • Yes, I wonder now how it's matter if: var x = (9/110).toFixed(3); console.log(x); console.log(x*24); console.log(x/45); console.log(x--); all of this works even if it is string. When does it matter? Thanks – kajojeq Nov 14 '13 at 14:59
  • @kajojeq, if you are doing like `(9/110).toFixed(3) === 0.082` it will return false – Smern Nov 14 '13 at 15:04
  • @smerny Yes you right. I am not often using `===` and `==` still gives true. thanks for fast reply! – kajojeq Nov 14 '13 at 15:08
  • *"When does it matter?"* Try `x + 42`. – Felix Kling Nov 14 '13 at 15:33
  • Right, added line with parsing to float if needed. Hope now It's clear. thanks – kajojeq Nov 14 '13 at 15:56
4

You can do this

Math.round(num * 1000) / 1000

This will round it correctly. If you wish to just truncate rather than actually round, you can use floor() instead of round()

Smern
  • 18,746
  • 21
  • 72
  • 90
4

Use this to round 0.818181... to 0.81:

x = 9/110;
Math.floor(x * 1000) / 1000;
Joren
  • 3,068
  • 25
  • 44
2

Try this

var num = x/y;
parseFloat((Math.round(num * 100) / 100).toPrecision(3))
Wallace Vizerra
  • 3,382
  • 2
  • 28
  • 29
  • 5
    You should be aware that this question is a year old, with an accepted answer and multiple possible duplicates. Therefore, you should take extra care to demonstrate how your answer is more useful than the already provided responses. – Claies Jun 09 '15 at 22:13
0

if i'm use the simple expression, result may be unexpected:

const fraction = (value) => {
  return value - Math.floor(value);
}
fraction(1 / 3);
=> 0.333333333333 // is Right
fraction(22 / 3);
=> 0.33333333333333304 // is Unexpected

So, my robust expression is:

const fraction = (value) => {
  return parseFloat(value.toString().replace(/^\d+$/, '0').replace(/^.+?(?=\.)/, ''))
}
fraction(22 / 3);
=> 0.333333333333333
fraction(1111)
=> 0
fraction(.122211)
=> 0.122211
fraction(11111.122211)
=> 0.122211
fraction(11111.12)
=> 0.12
fraction(11111.11112)
=> 0.11112
Sergio Belevskij
  • 2,478
  • 25
  • 24