dongtao5104 2015-02-18 14:59
浏览 51
已采纳

将PHP代码转换为Javascript以将数字读入句子

I want to convert my PHP code to JavaScript. The code's purpose is for read numbers into sentence. The problem is the sentence won't showing into input text. Here is the code:

JavaScript

function pembilang(angka) {
    var bilangan =[
            '',
            'SATU',
            'DUA',
            'TIGA',
            'EMPAT',
            'LIMA',
            'ENAM',
            'TUJUH',
            'DELAPAN',
            'SEMBILAN',
            'SEPULUH',
            'SEBELAS'
    ];

    if (angka < 12) {
        $('#NILAI_HURUF').val(bilangan[angka]);
    } else if (angka < 20) {
        $('#NILAI_HURUF').val(bilangan[angka - 10] + ' BELAS');
    } else if (angka < 100) {
        hasil_bagi == (angka / 10);
        hasil_mod == angka % 10;
        $('#NILAI_HURUF').val(trim(sprintf('%s PULUH %s', bilangan[hasil_bagi], bilangan[hasil_mod])));
    } else if (angka < 200) {
        $('#NILAI_HURUF').val('SERATUS %s', terbilang(angka - 100));
    } else if (angka < 1000) {
        hasil_bagi == (angka / 100);
        hasil_mod == angka % 100;
        $('#NILAI_HURUF').val(trim(sprintf('%s RATUS %s', $bilangan[hasil_bagi], terbilang(hasil_mod))));
    } else if (angka < 2000) {
        $('#NILAI_HURUF').val(trim(sprintf('SERIBU %s', terbilang(angka - 1000))));
    } else if (angka < 1000000) {
        hasil_bagi == (angka / 1000);
        hasil_mod == angka % 1000;
        $('#NILAI_HURUF').val(sprintf('%s RIBU %s', terbilang(hasil_bagi), terbilang(hasil_mod)));
    } else if (angka < 1000000000) {
        hasil_bagi == (angka / 1000000);
        hasil_mod == angka % 1000000;
        $('#NILAI_HURUF').val(trim(sprintf('%s JUTA %s', terbilang(hasil_bagi), terbilang(hasil_mod))));
    } else if (angka < 1000000000000) {
        hasil_bagi == (angka / 1000000000);
        hasil_mod == fmod(angka, 1000000000);
        $('#NILAI_HURUF').val(trim(sprintf('%s MILYAR %s', terbilang(hasil_bagi), terbilang(hasil_mod))));
    } else if (angka < 1000000000000000) {
        hasil_bagi == angka / 1000000000000;
        hasil_mod == fmod(angka, 1000000000000);
        $('#NILAI_HURUF').val(trim(sprintf('%s TRILIYUN %s', terbilang(hasil_bagi), terbilang(hasil_mod))));
    } else {
        alert('Too many digits.');
    }
}

Then this is where I input the numbers:

html

<input onkeypress="return isNumberKey(event);" onChange="pembilang(this.value);" class="form-control" placeholder="Masukan nilai angka..." name="NILAI_ANGKA_AKHIR" type="number" required>

And this is where the sentence must be displayed:

html

<input id="NILAI_HURUF" value="" class="form-control disabled" placeholder="Masukan no induk..." name="NILAI_HURUF" type="text" required>
  • 写回答

1条回答 默认 最新

  • doutongwei4380 2015-02-18 15:09
    关注

    Okay, I solved it by myself.

    I just give a variable like this into JavaScript function :

    angka = angka;
    

    The solution is implementing/add JavaScript module from phpjs.org/functions/sprintf. Then the code for displaying that sentences is like this:

    JavaScript

    function pembilang(angka) {
        var angka = angka;
    
        var bilangan =[
                '',
                'SATU',
                'DUA',
                'TIGA',
                'EMPAT',
                'LIMA',
                'ENAM',
                'TUJUH',
                'DELAPAN',
                'SEMBILAN',
                'SEPULUH',
                'SEBELAS'
        ];
    
        if (angka < 12) {
            $('#NILAI_HURUF').val(bilangan[angka]);
        } else if (angka < 20) {
            $('#NILAI_HURUF').val(bilangan[angka - 10] + ' BELAS');
        } else if (angka < 100) {
            hasil_bagi = Math.floor(angka / 10);
            hasil_mod = angka % 10;
            //   example 4: sprintf("%s", 123456789012345);
            var x = sprintf("%s PULUH %s", bilangan[hasil_bagi], bilangan[hasil_mod]);
            $('#NILAI_HURUF').val(x);
        } else if (angka < 101) {
            var x = sprintf("SERATUS %s", bilangan[angka - 100]);
            $('#NILAI_HURUF').val(x);
        }else {
            alert('Maksimal nilai 100.');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?