Home >Web Front-end >JS Tutorial >How to automatically convert entered numbers into currency format in Javascript (code attached)

How to automatically convert entered numbers into currency format in Javascript (code attached)

亚连
亚连Original
2018-05-18 13:53:493032browse

下面是我给大家整理的Javascript中自动将输入的数字转化成货币格式,有兴趣的同学可以去看看。

在Javascript中比较麻烦实现,要使用正则表达式进行转化

<script language="javascript">
function ParseMoney(str)
{
   var idx = str.indexOf(".");
   while (str.substring(0, idx++).length % 3) 
   {
    str = "0" + str;
   }
   return "¥" + str.replace(/(/d{3})/g, "$1,").replace(/,/./, ".").replace(/(^0*)|(,$)/g, "");
}
document.write("你的货币总数是:"+ParseMoney("123456789.365"));
</script>

但是在VBscript中有现成的函数进行转化

<script language="vbscript">
MsgBox FormatCurrency(123456789)
</script>

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

 如何使用vue.js实现价格格式化(代码附上)

js原生代码实现数据双向绑定(可以直接拿来使用,已经封装好了)

畅谈在原生JS与其他JS 区别

The above is the detailed content of How to automatically convert entered numbers into currency format in Javascript (code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn