-2

Μ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>
Community
  • 1
  • 1
  • Looks lit it works in your example. What do you call "a word" ? When I type "33" it works but "33a" is NaN. For me, your example transforms an "int" to a "string" – Hearner Aug 08 '18 at 07:36
  • Is there any error messages in your browser console? – Thum Choon Tat Aug 08 '18 at 07:36
  • 3
    In what way doesn't it work? What was it supposed to do, what did it actually do? Were there error messages? See [mcve]. – peeebeee Aug 08 '18 at 07:36
  • 'Amount in letters' sounds like you want the real word of the number, (like Two, instead of 2) that's not possible to do with toString, which converts the value only to a string of the number. (making it from `2` to `"2"`) – Steven Aug 08 '18 at 07:37
  • 1
    I presume that when you say "into words" you mean `55` to `"fifty five"`? – OliverRadini Aug 08 '18 at 07:37
  • when the value is say `null`, `toString()` won't work, use `String(..code..)` instead... or do a check for `null` – Ashish Ranjan Aug 08 '18 at 07:38
  • yes oliverRadini i going right hear plz suggest me where i go wrong – Mõña Singh Aug 08 '18 at 07:39
  • 1
    Possible duplicate of [Convert digits into words with JavaScript](https://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript) – Hearner Aug 08 '18 at 07:42
  • Please edit your answer saying "When I put in X I expect Y but instead I'm getting Z". People get frustrated trying to figure out what you mean by "it doesn't work". – Dominic Aug 08 '18 at 07:42
  • Possible duplicate of [Number to Word - jquery](https://stackoverflow.com/questions/7257575/number-to-word-jquery) – Jithin Raj P R Aug 08 '18 at 07:43

1 Answers1

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());

Happy Codeing!!

  • its helpfull mahabub-rabbani but still i get 1=1 and not find my answer 1=one like that – Mõña Singh Aug 08 '18 at 08:11
  • it is working but show on toolbar like notifications "come" and then click on it give the result i want only in amount in letters field plz try to understand – Mõña Singh Aug 08 '18 at 09:04
  • i got it thaku so much Mahabub Rabbani.... i solve my biggest problem today thanks again.. it work properly as i required.. – Mõña Singh Aug 08 '18 at 09:21
  • hey Mahabub Rabbani can u tell me how i covert this in south Asian number system like lakes, crore etc – Mõña Singh Aug 21 '18 at 08:44
  • Change scale words.... and change for loop as your requirement if you wants to see as 1 million to 10 lakhs – Mahabub Rabbani Aug 22 '18 at 12:44
  • i will change the scale but cant be able to change the loop i try some changes but return wrong vale all the time plz see below my code @Mahabub Rabbani – Mõña Singh Sep 08 '18 at 05:33