1.HTML代码(使用了angular,但不影响操作)

                            <div class="form-group"  ng-show="Model.IsCash==1">
                                <label class="control-label col-xs-1">合同金额:</label>
                                <div class="col-xs-4">
                                    <input class="form-control text-box single-line ng-pristine ng-scope ng-not-empty ng-valid-maxlength ng-valid-min ng-dirty ng-valid ng-valid-max ng-touched" type="number" ng-blur="NumChange($event)" required ng-model="Model.TotalAmount" min="0" max="100000000000"  />
                                </div>
                                <label class="control-label col-xs-1">大写:</label>
                                <div class="col-xs-4">
                                    <input class="form-control text-box single-line" type="text" required ng-model="Model.TotalAmountCapital" maxlength="50" />
                                </div>
                            </div>

2.JavaScript代码(同样使用了angular……)

                //监控金额
                $scope.NumChange = function ($event) {
                    if ($($event.target).val() == '' || $($event.target).val() > 999999999999.99) {
                        $($event.target).val("0");
                        $scope.Model.TotalAmount = 0;
                    }
                    $scope.Model.TotalAmount = Math.floor($($event.target).val() * 100) * 1.0 / 100;
                    $scope.UpNum();
                }
                //金额大写
                $scope.UpNum = function () {
                    var m = $scope.Model.TotalAmount;
                    if (!(m === undefined || m === '')) {
                        m = parseFloat(toFixed2(m)).toFixed(2);//强制保留两位小数,不够补零
                        m = parseInt(m * 100);//预防m * 100后面会出现未知小数部分,如26.80*100=2680.0……003
                        m += "";
                        var length = m.length;
                        var unit = new Array("仟", "佰", "拾", "", "仟", "佰", "拾", "", "仟", "佰", "拾", "", "角", "分");
                        var result = "";
                        for (var i = 0; i < length; i++) {
                            if (i == 2) {
                                result = "元" + result;
                            } else if (i == 6) {
                                result = "万" + result;
                            } else if (i == 10) {
                                result = "亿" + result;
                            }
                            if (m.charAt(length - i - 1) == 0) {
                                if (i != 0 && i != 1) {
                                    if (result.charAt(0) != '零' && result.charAt(0) != '元' && result.charAt(0) != '万') {
                                        result = "零" + result;
                                    }
                                }
                                continue;
                            }
                            result = toCapital(m.charAt(length - i - 1)) + unit[unit.length - i - 1] + result;
                        }
                        result += result.charAt(result.length - 1) == '元' ? "整" : "";
                        $scope.Model.TotalAmountCapital = result;
                    } else {
                        $scope.Model.TotalAmountCapital = '';
                    }
                }
                var toFixed2 = function (value) {
                    var value = Math.round(parseFloat(value) * 100) / 100;
                    var xsd = value.toString().split(".");
                    if (xsd.length == 1) {
                        value = value.toString() + ".00";
                        return value;
                    }
                    if (xsd.length > 1) {
                        if (xsd[1].length < 2) {
                            value = value.toString() + "0";
                        }
                        return value;
                    }
                }
                var toCapital = function (n) {
                    switch (n) {
                        case "0":
                            return "零";
                        case "1":
                            return "壹";
                        case "2":
                            return "贰";
                        case "3":
                            return "叁";
                        case "4":
                            return "肆";
                        case "5":
                            return "伍";
                        case "6":
                            return "陆";
                        case "7":
                            return "柒";
                        case "8":
                            return "捌";
                        case "9":
                            return "玖";
                    }
                }

3.页面效果

 4.总结:

目前是针对保留两位小数装换金额的,如需保留更多位小数,可仿造进行适当的修改

01-18 13:37