Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dogbert's answer is good, but if your code might have to deal with negative numbers, <code>Math.floor</code> by itself may give unexpected results.</p> <p>E.g. <code>Math.floor(4.3) = 4</code>, but <code>Math.floor(-4.3) = -5</code></p> <p>Use a helper function like this one instead to get consistent results:</p> <pre><code>truncateDecimals = function (number) { return Math[number &lt; 0 ? 'ceil' : 'floor'](number); }; // Applied to Dogbert's answer: var a = 5.467; var truncated = truncateDecimals(a * 100) / 100; // = 5.46 </code></pre> <p>Here's a more convenient version of this function:</p> <pre><code>truncateDecimals = function (number, digits) { var multiplier = Math.pow(10, digits), adjustedNum = number * multiplier, truncatedNum = Math[adjustedNum &lt; 0 ? 'ceil' : 'floor'](adjustedNum); return truncatedNum / multiplier; }; // Usage: var a = 5.467; var truncated = truncateDecimals(a, 2); // = 5.46 // Negative digits: var b = 4235.24; var truncated = truncateDecimals(b, -2); // = 4200 </code></pre> <p>If that isn't desired behaviour, insert a call to <code>Math.abs</code> on the first line:</p> <pre><code>var multiplier = Math.pow(10, Math.abs(digits)), </code></pre> <p><strong>EDIT:</strong> shendz correctly points out that using this solution with <code>a = 17.56</code> will incorrectly produce <code>17.55</code>. For more about why this happens, read <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="noreferrer">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>. Unfortunately, writing a solution that eliminates all sources of floating-point error is pretty tricky with javascript. In another language you'd use integers or maybe a Decimal type, but with javascript...</p> <p>This solution <em>should be</em> 100% accurate, but it will also be slower:</p> <pre><code>function truncateDecimals (num, digits) { var numS = num.toString(), decPos = numS.indexOf('.'), substrLength = decPos == -1 ? numS.length : 1 + decPos + digits, trimmedResult = numS.substr(0, substrLength), finalResult = isNaN(trimmedResult) ? 0 : trimmedResult; return parseFloat(finalResult); } </code></pre> <p>For those who need speed but also want to avoid floating-point errors, try something like <a href="https://github.com/dtrebbien/BigDecimal.js" rel="noreferrer">BigDecimal.js</a>. You can find other javascript BigDecimal libraries in this SO question: <a href="https://stackoverflow.com/q/744099/224354">"Is there a good Javascript BigDecimal library?"</a> and here's a good blog post about <a href="http://blog.smartbear.com/testing/four-serious-math-libraries-for-javascript/" rel="noreferrer">math libraries for Javascript</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload