安裝 wavesurfer.js 在項目中安裝 wavesurfer.js npm install --save wavesurfer.js 常規方式引入 如果你的根目錄中沒有 components 目錄則需要創建該目錄,併在此目錄中創建 WaveSurfer.vue 內容如下: <template ...
朋友是做財務的,由於工作時間不長,每次將數字轉為大寫人民幣的時候,總是提心吊膽的,生怕弄錯了。而且,如果是整數的話,還好,但是有小數的時候就比較費勁了,比如123.45,轉成大寫應該是壹佰貳拾叄元肆角伍分
。然後就找到我,問我有沒有什麼辦法?
簡單啊,做一個轉換程式不就可以了嘛?而且網上現成的一大把。出於好玩,所以使用Vue做了一個人民幣大寫轉換器,在https的支持下,可以使用複製,朗讀功能。還貼心的做了一個大寫人民幣參照表。
先看一眼成品
下麵說說代碼,及其簡單哦!
創建一個Vue工程,在main.js
中引入ant design
import App from './App.vue'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.less'
Vue.use(Antd)
直接再App.vue上完成吧。
HTML代碼:
<template>
<div id="app">
<div class="container">
<div class="main box mt-4">
<h1 class="text-center">人民幣大寫轉換器</h1>
<hr />
<a-row type="flex" justify="space-between" align="top" :gutter="[16,16]">
<a-col :md="16">
<div class="screen">
<div class="cap-result border bg-light mb-2 p-3">
<h3>{{Result}}</h3>
</div>
<a-row type="flex" justify="space-between" align="middle">
<a-col :md="16">
<a-input placeholder="輸入數字金額" id="input-amount-number" class="form-control" v-model="InputAmount" @change="rmbCap"></a-input>
</a-col>
<a-col :md="8" class="text-right">
<a-space>
<a-button type="primary" @click="CopyResult">複製</a-button>
<a-button @click="ReadAloud">朗讀</a-button>
<a-button type="danger" @click="ClearInput">清除</a-button>
</a-space>
</a-col>
</a-row>
</div>
<div class="keypad">
<a-row type="flex" justify="start" align="middle" :gutter="[24,16]">
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('1')">1</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('2')">2</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('3')">3</button>
</a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[24,16]">
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('4')">4</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('5')">5</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('6')">6</button>
</a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[24,16]">
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('7')">7</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('8')">8</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('9')">9</button>
</a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[24,16]">
<a-col :span="16">
<button class="btn btn-light key" v-on:click="KeyPadClicked('0')">0</button>
</a-col>
<a-col :span="8">
<button class="btn btn-light key" v-on:click="KeyPadClicked('.')">.</button>
</a-col>
</a-row>
</div>
</a-col>
<a-col :md="8">
<a-card title="參照表">
<a-row type="flex" justify="start" align="middle" :gutter="[16,16]">
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">零</div><div class="ref-text-ch">0</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">壹</div><div class="ref-text-ch">1</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">貳</div><div class="ref-text-ch">2</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">叄</div><div class="ref-text-ch">3</div></div></a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[16,16]">
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">肆</div><div class="ref-text-ch">4</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">伍</div><div class="ref-text-ch">5</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">陸</div><div class="ref-text-ch">6</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">柒</div><div class="ref-text-ch">7</div></div></a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[16,16]">
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">捌</div><div class="ref-text-ch">8</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">玖</div><div class="ref-text-ch">9</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">拾</div><div class="ref-text-ch">10</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">佰</div><div class="ref-text-ch">百</div></div></a-col>
</a-row>
<a-row type="flex" justify="start" align="middle" :gutter="[16,16]">
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">仟</div><div class="ref-text-ch">千</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">萬</div><div class="ref-text-ch">萬</div></div></a-col>
<a-col :span="6"><div class="ref-text-box"><div class="ref-text-cap">億</div><div class="ref-text-ch">億</div></div></a-col>
</a-row>
</a-card>
</a-col>
</a-row>
</div>
</div>
<div class="mt-4 text-center">by DotNet寶藏庫</div>
</div>
</template>
腳本代碼:
<script>
function isEmpty(str) {
if (str == 'undefined' || !str || !/[^\s]/.test(str)) {
return true;
} else {
return false;
}
}
import Long from 'long'
export default {
name: 'App',
data(){
return{
Result:"",
InputAmount:"",
}
},
components: {
},
created(){
document.title = "人民幣大寫轉換器";
},
methods:{
KeyPadClicked:function(num){
let that = this;
//this.InputAmount += num
let tmp = num;
switch(num){
case "0" :
if(that.InputAmount!="0"){
tmp = 0;
}
break;
case ".":
if(that.InputAmount.indexOf('.')==-1){
tmp = "."
}
break;
}
that.InputAmount +=tmp;
var value = that.InputAmount;
if(value.length<=11){
if(!isEmpty(value) && value.indexOf('.')!=-1){
var parts = value.split('.');
var desPart = parts[1];
if(desPart.length<=2){
if(value=='.'){
value = "0.";
}
}
}
if(value.startsWith(0) && value.indexOf('.')==-1){
value = parseInt(value).toString();
}
}
that.InputAmount = value;
this.rmbCap();
},
ClearInput:function(){
this.InputAmount = ""
this.rmbCap()
},
CopyResult(){
let text = this.Result;
let that = this;
if(!isEmpty(text)){
navigator.clipboard.writeText(text).then(function(){
that.$message.success('複製成功!')
}).catch(function(error){
that.$message.error(error);
})
}
},
ReadAloud(){
let text = this.Result;
if(!isEmpty(text)){
let utterance = new SpeechSynthesisUtterance();
utterance.text = text;
utterance.lang = "zh-CN";
const synth = window.speechSynthesis;
synth.speak(utterance);
}
},
rmbCap:function(){
console.log(this.InputAmount);
this.Result = isEmpty(this.InputAmount)?"":this.RMBToCap(this.InputAmount);
},
RMBToCap(input){
let that = this;
var MAXIMUM_NUMBER = 99999999999.99;
var CN_ZERO = "零";
var CN_ONE = "壹";
var CN_TWO = "貳";
var CN_THREE = "叄";
var CN_FOUR = "肆";
var CN_FIVE = "伍";
var CN_SIX = "陸";
var CN_SEVEN = "柒";
var CN_EIGHT = "捌";
var CN_NINE = "玖";
var CN_TEN = "拾";
var CN_HUNDRED = "佰";
var CN_THOUSAND = "仟";
var CN_TEN_THOUSAND = "萬";
var CN_HUNDRED_MILLION = "億";
//var CN_SYMBOL = "人民幣";
var CN_DOLLAR = "元";
var CN_TEN_CENT = "角";
var CN_CENT = "分";
var CN_INTEGER = "整";
if(parseFloat(input)>MAXIMUM_NUMBER){
that.$message.error('金額必須小於一百億元');
return ''
}
var integral;
var decimalPart;
var parts = input.split('.');
if(parts.length>1){
integral = parts[0];
decimalPart = parts[1]
if(isEmpty(decimalPart)){
decimalPart = "00";
}
if(decimalPart.length==1){
decimalPart+=0;
}
decimalPart = decimalPart.substr(0,2);
}
else{
integral = parts[0];
decimalPart = "";
}
var digits = [CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE]
var radices = ['', CN_TEN, CN_HUNDRED, CN_THOUSAND]
var bigRadices = ['',CN_TEN_THOUSAND, CN_HUNDRED_MILLION]
var decimals = [CN_TEN_CENT, CN_CENT]
var outputCharacters = "";
if(Long.fromValue(integral)>0){
var zeroCount=0;
for (var i = 0; i < integral.length; i++){
var p = integral.length - i - 1;
var d = integral.substr(i, 1);
var quotient = p / 4;
var modulus = p % 4;
if (d == "0"){
zeroCount++;
}else{
if (zeroCount > 0){
outputCharacters += digits[0];
}
zeroCount = 0;
outputCharacters += digits[parseInt(d)] + radices[modulus];
}
if (modulus == 0 && zeroCount < 4){
outputCharacters += bigRadices[quotient];
zeroCount = 0;
}
}
outputCharacters += CN_DOLLAR;
}
if (!isEmpty(decimalPart)){
for (var ii = 0; ii < decimalPart.length; ii++){
var dd = decimalPart.substr(ii, 1);
if (dd != "0"){
outputCharacters += digits[parseInt(dd)] + decimals[ii];
}
}
}
if (isEmpty(outputCharacters)){
outputCharacters = CN_ZERO + CN_DOLLAR;
}
if (isEmpty(decimalPart)){
outputCharacters += CN_INTEGER;
}
return outputCharacters;
}
}
}
</script>
CSS
<style>
@import "./assets/app.css";
</style>
app.css
body {margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem !important;font-weight:400;line-height:1.5;color:#212529 !important;text-align:left;background:#F2F2F2 !important;}
*,::after,::before {box-sizing:border-box;}
.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6 {margin-bottom:.5rem;font-weight:500;line-height:1.2;}
h1,h2,h3,h4,h5,h6 {margin-top:0;margin-bottom:.5rem !important;}
.h3,h3 {font-size:1.75rem;}
.container {width:100%;padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}
@media (min-width:576px) {.container {max-width:540px}
}
@media (min-width:768px) {.container {max-width:720px}
}
@media (min-width:992px) {.container {max-width:960px}
}
@media (min-width:1200px) {.container {max-width:1140px}
}
article,aside,figcaption,figure,footer,header,hgroup,main,nav,section {display:block;}
:root {--transparent-dark-1:rgba(0,0,0,.108);--transparent-dark-2:rgba(0,0,0,.125);--transparent-dark-3:rgba(0,0,0,.132);--transparent-dark-4:rgba(0,0,0,.175);--gray-1:#f2f2f2;--gray-2:#eee;}
.box {background-color:#FFFFFF;border:1px solid var(--gray-2);padding:1.8rem;box-shadow:0 1.6px 3.6px 0 var(--transparent-dark-3),0 .3px .9px 0 var(--transparent-dark-1);border-radius:3px;}
.mt-4 {margin-top:1.5rem;}
.text-center {text-align:center;}
.text-right {text-align:right;}
.h1,h1 {font-size:2.5rem;}
hr {box-sizing:content-box;height:0;overflow:visible;margin-top:1rem;margin-bottom:1rem;border:0;border-top:1px solid rgba(0,0,0,.1);}
.bg-light {background-color:#f8f9fa!important;}
.border {border:1px solid #dee2e6!important;}
.mb-2,.my-2 {margin-bottom:.5rem!important;}
.p-3 {padding:1rem!important;}
.cap-result {min-height:75px;}
.w-100 {width:100%!important;}
.mr-3,.mx-3 {margin-right:1rem!important;}
.ref-text-box {display:flex;flex-direction:column;background-color:#F2F2F2;text-align:center;height:4.4rem;width:4.4rem;justify-items:center;justify-content:center;align-items:center;}
.ref-text-cap {font-size:125%;}
.ref-text-ch {color:#888888;}
.keypad {margin-top:1rem;}
.btn {margin:0;text-transform:none;display:inline-block;cursor:pointer;font-weight:400;color:#212529;text-align:center;vertical-align:middle;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-color:transparent;border:1px solid transparent;padding:.375rem .75rem;font-size:1rem;line-height:1.5;border-radius:.25rem;transition:color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;}
.btn-light {color:#212529;background-color:#f8f9fa;border-color:#f8f9fa;}
.btn-light:hover {color:#212529;background-color:#e2e6ea;border-color:#dae0e5;}
.key {font-family:"Consolas";font-size:250%;width:100%;min-height:100px;}
.form-control {height:calc(1.5em + .75rem + 5px) !important;padding:.375rem .75rem !important;font-size:1rem !important;}
歡迎大家關註我的微信公眾號,一起進步,一起成長下載方式:
掃描公眾號二維碼關註我,回覆rmb
下載,壓縮包包含了驗證碼類庫、服務端API、HTML+JQuery完整代碼、vue3組件代碼及演示代碼!