Example of digital formatting functionality implemented by JS

  • 2021-07-18 07:06:53
  • OfStack

This article describes the example of JS to achieve the digital formatting function. Share for your reference, as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Digital formatting </title>
<script type="text/javascript" language="javascript" >
//n Is the number to convert / A numeric string, s Is the unit to be added later, can be blank 
function num2money(n, s){ 
  if (typeof s == "undefined") 
    s = "";
  n = parseInt(n);
  n = parseInt(n) + Number(((n - parseInt(n)) + "").substring(0, 3));
  if (n / 1000 >= 1) {
    var nn = '' + n;
    nn = nn.substring(nn.length - 3, nn.length);
    s = ',' + nn + s;
    return num2money(parseInt(n / 1000), s);
  }
  else {
    return n + s;
  }
}
document.write(num2money("123.45", " US dollar "));// Output: 123 US dollar 
</script>
</head>
<body>
</body>
</html>

PS: Here are some computing tools recommended for your reference in the next step:

On-line 1 yuan function (equation) solution calculation tool;
http://tools.ofstack.com/jisuanqi/equ_jisuanqi

Scientific calculator online _ advanced calculator online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online Calculator _ Standard Calculator:
http://tools.ofstack.com/jisuanqi/jsq

More readers interested in JavaScript can check out the topics of this site: "Summary of JavaScript Mathematical Operation Usage", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Array Operation Skills", "Summary of JavaScript Sorting Algorithm", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of JavaScript Search Algorithm Skills" and "Summary of JavaScript Error and Debugging Skills"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: