That’s even simpler:
function truncateToDecimals(num, dec = 2) {
const calcDec = Math.pow(10, dec);
return Math.trunc(num * calcDec) / calcDec;
}
So:
truncateToDecimals(123456.786) -> 123456.78
More Related Contents:
- This doesn’t seem right? Javascript isn’t working
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- Why do regex constructors need to be double escaped?
- console.log of element.children shows 0 length but has three entries when expanded later
- Get cookie by name
- Array.fill(Array) creates copies by references not by value [duplicate]
- How to append text to a “?
- Relation between CommonJS, AMD and RequireJS?
- JavaScript – Owner of “this”
- Proper use of const for defining functions
- How to load image files with webpack file-loader
- setTimeout() inside JavaScript Class using “this”
- Convert simple array into two-dimensional array (matrix)
- Create table with jQuery – append
- Getting Chrome to prompt to save password when using AJAX to login
- javascript change background color on click [closed]
- Send a message with Discord.js
- Phone mask with jQuery and Masked Input Plugin
- Add elements to the DOM given plain text HTML using only pure JavaScript (no jQuery)
- How can I open an external link in Safari not the app’s UIWebView?
- Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00
- angularJS display base64 image
- Screen Scraping from a web page with a lot of Javascript [closed]
- Difference between RequireJS and CommonJS
- How to get the currently opened tab’s URL in my page action popup?
- Why is node.js asynchronous?
- Trying to load local JSON file to show data in a html page using JQuery
- How to get address location from latitude and longitude in Google Map.? [closed]
- Firebase TypeError: Cannot read property ‘val’ of undefined
- Javascript, why treated as octal