朋友是做财务的,由于工作时间不长,每次将数字转为大写人民币的时候,总是提心吊胆的,生怕弄错了。而且,如果是整数的话,还好,但是有小数的时候就比较费劲了,比如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组件代码及演示代码!

欢迎大家关注我的微信公众号,一起进步,一起成长
posted on 2023-01-13 21:17  gmval  阅读(811)  评论(0编辑  收藏  举报