用于转换字符串中的数字的javascript代码

问题描述 投票:-2回答:1

Μy code does not convert a number into words. Ιs there any mistake??a form related issues in django

<html>
<script>
$(document).ready(function () {
    $("#amount").keyup(function () {
        $("#amount_string").val(Number($("#amount").val()).toString());
    });
});
</script>
<form action="#" method="post">
    <p id="namecont">name: <input type="text" id="name"/></p> 
    <p>Date: <input type="text" id="datepicker"/></p>
    <p>Amount: <input type="text" id="amount" /></p>
    <p>Amount in Letters: <input id="amount_string" /></p>

</form>
</html>
javascript jquery
1个回答
1
投票
if you means to : 2 will show as "two"

then code should be like that...Note(using ourcodeworld code)

 $(document).ready(function () {
            $("#amount").keyup(function () {
                alert("come");
                var word = numbertoWord($("#amount").val());

                alert(word);
            });

            function numbertoWord(n) {

                var string = n.toString(),
                    units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words;

                var and = ' ' || 'and';

                if (parseInt(string) === 0) {
                    return 'zero';
                }

                units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];

                tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

                scales = ['', 'thousand', 'million', 'billion', 'trillion'];

                start = string.length;
                chunks = [];
                while (start > 0) {
                    end = start;
                    chunks.push(string.slice((start = Math.max(0, start - 3)), end));
                }

                chunksLen = chunks.length;
                if (chunksLen > scales.length) {
                    return '';
                }

                words = [];
                for (i = 0; i < chunksLen; i++) {

                    chunk = parseInt(chunks[i]);

                    if (chunk) {

                        ints = chunks[i].split('').reverse().map(parseFloat);

                        if (ints[1] === 1) {
                            ints[0] += 10;
                        }

                        if ((word = scales[i])) {
                            words.push(word);
                        }

                        if ((word = units[ints[0]])) {
                            words.push(word);
                        }

                        if ((word = tens[ints[1]])) {
                            words.push(word);
                        }

                        if (ints[0] || ints[1]) {

                            if (ints[2] || !i && chunksLen) {
                                words.push(and);
                            }

                        }

                        if ((word = units[ints[2]])) {
                            words.push(word + ' hundred');
                        }

                    }

                }

                return words.reverse().join(' ');

            }
        });

otherwise your code is perfect but why you use Number it should look like this

 $("#amount_string").val($("#amount").val());

快乐编码!!

© www.soinside.com 2019 - 2024. All rights reserved.