How to use division in JavaScript

Dayzza picture Dayzza · Sep 7, 2011 · Viewed 72k times · Source

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?

Answer

Charles Ma picture Charles Ma · Sep 7, 2011

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