Dave's Brain

Browse - Programming Tips - javaScript: how can I limit the number of decimal digits when displaying a number?

Date: 2018apr7
Language: javaScript
Keywords: dollars, cents, financial, round

Q.  javaScript: how can I limit the number of decimal digits when displaying a number?

A.  Use Number.toFixed() like this

const n = 1.23456789; console.log(n.toFixed(2));
Will display
1.23
Note that toFixed() requires a number and it returns a string. More info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.