Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the regular expression of converting the format of number and amount by JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces JavaScript how to achieve the regular expression of digital, amount format conversion, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Regular expression for conversion of numeric and amount formats

Input input real-time judgment input is in amount format

Function checkInput (obj) {var t = obj.value.charAt (0); obj.value = obj.value.replace (/ [^\ d.] / g, "); / / clear" number "and". " Characters other than obj.value = obj.value.replace (/ ^ [0] + [0-9] * $/ gi, ""); / / the first digit cannot be 0 obj.value = obj.value.replace (/ ^\. / g, ""); / / verify that the first character is a number instead of. Obj.value = obj.value.replace (/\. {2,} / g, "."); / / keep only the first one. Clear the excess obj.value = obj.value.replace (".", "$# $"). Replace (/\. / g, "). Replace (" $# $",". "); obj.value = obj.value.replace (/ ^ (\ -) * (\ d+)\. (\ d\ d). * $,'$1 $2.03') / / you can enter only two decimal if (t = ='-') {obj.value ='-'+ obj.value;}}

Convert the number to the amount format and retain two decimal places: convert 1234567 to 1234567.00

/ / s is a number, n is the number of decimal places function fmoney (s, n) {n = n > 0 & & n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report