# Two common ways to convert lowercase amounts to uppercase amounts using JavaScript

Sun Shoushou's Essays 2021-09-15 10:43:58

Catalog

design sketch :

Method 1 : Using regular expressions

Method 2 : routine JavaScript Realization

### design sketch : Front lowercase , After capital , Use here vue test , The same applies to other frameworks . Lowercase is the value field money, Call the function directly in uppercase dealBigMoney(money) that will do .

### Method 1 : Using regular expressions

``````dealBigMoney(n) {
if (!/^(0|[1-9]\d*)(\.\d+)?\$/.test(n))
return " data is invalid ";
let unit = " Ten billion yuan and cents ", str = "";
n += "00";
let p = n.indexOf('.');
if (p >= 0){
n = n.substring(0, p) + n.substr(p+1, 2);
unit = unit.substr(unit.length - n.length);
}
for (var i=0; i < n.length; i++)
str += ' One two three four five six seven eight nine '.charAt(n.charAt(i)) + unit.charAt(i);
return str.replace(/ zero ( thousand | hundred | Ten | horn )/g, " zero ").replace(/( zero )+/g, " zero ").replace(/ zero ( ten thousand | Billion | element )/g, "\$1").replace(/( Billion ) ten thousand | one ( Ten )/g, "\$1\$2").replace(/^ Yuan zero ?| Zero point /g, "").replace(/ element \$/g, " Yuan only ");
}
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
```

Template call

<el-col>{{money}}|{{dealBigMoney(money) }}</el-col>

### Method 2 : routine JavaScript Realization

``````/** Number amount in words conversion ( Can handle integers , decimal , negative ) */
dealBigMoney(n)
{
var fraction = [' horn ', ' branch '];
var digit = [' zero ', ' one ', ' Ii. ', ' 3 ', ' boss ', ' wu ', ' lu ', ' Retailer, ', ' ', ' nine '];
var unit = [ [' element ', ' ten thousand ', ' Billion '], ['', ' Ten ', ' Bai ', ' Thousand '] ];
var head = n < 0? ' Owe ': '';
n = Math.abs(n);
var s = '';
for (var i = 0; i < fraction.length; i++)
{
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/ zero ./, '');
}
s = s || ' whole ';
n = Math.floor(n);
for (var i = 0; i < unit.length && n > 0; i++)
{
var p = '';
for (var j = 0; j < unit.length && n > 0; j++)
{
p = digit[n % 10] + unit[j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/( zero .)* zero \$/, '').replace(/^\$/, ' zero ') + unit[i] + s;
}
return head + s.replace(/( zero .)* zero yuan /, ' element ').replace(/( zero .)+/g, ' zero ').replace(/^ whole \$/, ' Zero integer ');
} ```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
```

This is the same , This kind of tool class is easy to use , If there is a better way, welcome to leave a message !