Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-01 04:34:47 09月28日 Update

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

Tags: Number format amount article regular expression decimal input character decimal point two value digits interest at the same time real-time more friends knowledge ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Apple Shulou Technology NVidia