converting integer input to words

霸气de小男生 提交于 2019-12-11 20:27:20

问题


I have to write a program such that I give it a three digit input and it converts it into word format. example:

input = 907
output = nine hundred seven

I can easily figure out how to give the hundred position output. I can divide the input by 100 and then store the whole part of result into a new variable. and then if the variable has value, let's say 7 make another new variable of character type and give it a value seven using if conditionals.

But I can't figure out what to do for tens and ones. I mean when there is a number like 907, do I write something so that it gives me the correct output. PS I just started programming and don't know the best method to do this.

What is the correct way to handle this?


回答1:


There are good libraries they can perform this kind of task for you. However you can get the result using :

int intput = 907;
int hundred = input/100;
int ten = input/10%10;
int one = input%10;

To use in loops you can use:

int one = input%10;
int ten = input/10%10;
int hundred = input/10/10%10;
int thousand = input/10/10/10%10;
.....



回答2:


I use this:

// Convert numbers to words
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown (you can change the numbering system if you wish)
// American Numbering System
var th = ['', 'thousand', 'million', 'billion', 'trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var tn = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
var tw = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

function convertNumberToString(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return 'not a number';
    var x = s.indexOf('.');
    if (x == -1) x = s.length;
    if (x > 15) return 'too big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == '1') {
                str += tn[Number(n[i + 1])] + ' ';
                i++;
                sk = 1;
            } else if (n[i] != 0) {
                str += tw[n[i] - 2] + ' ';
                sk = 1;
            }
        } else if (n[i] != 0) {
            str += dg[n[i]] + ' ';
            if ((x - i) % 3 == 0) str += 'hundred ';
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + ' ';
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
    }
    return str.replace(/\s+/g, ' ');
}


来源:https://stackoverflow.com/questions/12332979/converting-integer-input-to-words

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!