73

I want to divide a number in JavaScript and it would return a decimal value.

For example: 737/1070 - I want JavaScript to return 0.68; however it keeps rounding it off and return it as 0.

How do I set it to return me either two decimals place or the full results?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Dayzza
  • 1,561
  • 7
  • 18
  • 29

6 Answers6

89

Make one of those numbers a float.

737/parseFloat(1070)

or a bit faster:

737*1.0/1070

convert to 2 decimal places

Math.round(737 * 100.0 / 1070) / 100
Charles Ma
  • 47,141
  • 22
  • 87
  • 101
  • 3
    `parseFloat` is for parsing from a string to a float. – Jamiec Sep 07 '11 at 09:57
  • 9
    a pair of pliers works just fine for banging in a nail, doesn't mean you should use it for such :) – Jamiec Sep 07 '11 at 10:14
  • Why not write `737.0 / 1070.0`? – Jonathan Leffler Nov 29 '11 at 03:29
  • 2
    @JonathanLeffler because it might be stored in a variable. – Charles Ma Nov 29 '11 at 21:29
  • 2
    In which case, the `* 1.0` is probably the way I'd do it. I must have been taking a too literal interpretation of the question which was explicitly working with integer constants. – Jonathan Leffler Nov 29 '11 at 21:32
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat `parseFloat(string)`, you spread bad usage of parseFloat to the whole internet – Christophe Roussy Jun 07 '17 at 12:15
  • 1
    @JonathanLeffler +1 for `* 1.0`. Should we edit this answer to make it appear as the preferred solution? This answer is the first result for "javascript float division" search on my search engine. – Jérôme Jul 03 '17 at 20:20
  • @Jérôme: Best ask someone with a gold, or perhaps silver, badge in Javascript. I don't have even a bronze badge over here. I don't feel qualified to make that judgement call. – Jonathan Leffler Jul 03 '17 at 20:38
23

(737/1070).toFixed(2); rounds the result to 2 decimals and returns it as a string. In this case the rounded result is 0.69 by the way, not 0.68. If you need a real float rounded to 2 decimals from your division, use parseFloat((737/1070).toFixed(2))

See also

KooiInc
  • 119,216
  • 31
  • 141
  • 177
3

Also you can to use [.toPrecision(n)], where n is (total) the number of digits. So (23.467543).toPrecision(4) => 23.47 or (1241876.2341).toPrecision(8) => 1241876.2.

See MDN

KooiInc
  • 119,216
  • 31
  • 141
  • 177
Vladimir
  • 31
  • 1
  • 5
    Be careful with `toPrecision`, it is a total number of digits, not the number of digits following the decimal (`.`). See this SO question: https://stackoverflow.com/questions/3337849/difference-between-tofixed-and-toprecision – kbpontius Oct 25 '19 at 01:22
2

Try this

 let ans = 737/1070;
 console.log(ans.toFixed(2));

toFixed() function will do

elraphty
  • 630
  • 6
  • 13
0

with lodash:

const _ = require("lodash"); 

Use of _.divide() method

let gfg = _.divide(12, 5);

     

Printing the output

console.log(gfg)
2.4

credit

Embedded_Mugs
  • 2,132
  • 3
  • 21
  • 33
0

to get it to 2 decimal places you can: alert( Math.round( 737 / 1070 * 100) / 100 )

Billy Moon
  • 57,113
  • 24
  • 136
  • 237