2011-07-08 18 views
5

Chỉ cần tự hỏi nếu có bất kỳ thư viện mà có thể dễ dàng biến số vào cơ quan đại diện chuỗi của họ trong javascript, jquery, php, python vv ...Số để String - Làm thế nào để biến 150 thành "Một trăm năm mươi"

Ví dụ: Tôi cần phải biến 1.210 thành "Một nghìn hai trăm và mười".

Có tên cho quy trình này không?

+1

Sách Thuật toán Unplugged (http://www.amazon.com/Algorithms-Unplugged-Berthold-V%C3%B6cking/dp/3642153275) đề cập đến vấn đề này một cách tốt đẹp. – 01es

Trả lời

2

Giải pháp Javascript. Hỗ trợ tất cả các số nguyên từ vô cùng tiêu cực đến vô cùng:

var GetWord; 
(function(){ 
    GetWord = function (number) {//number must be an integer 
     return (function f(number){ 
      var output = []; 
      var is_negative = number < 0; 
      number = Math.abs(number); 

      var chunks = []; 
      for (var x = count_names.length - 1, limit = 0; x >= limit; --x) { 
       var chunk = Math.floor(number/counts[x]); 
       if (chunk !== 0) { 
        output.push(GetWord(chunk) + " " + count_names[x]); 
       } 
       chunks[x] = chunk; 
       number -= chunk * counts[x]; 
      } 
      if (number !== 0) { 
       if (number <= 19) { 
        output.push(number_names[number]); 
       } else { 
        var tens = Math.floor(number/10); 
        number -= tens * 10; 
        if (number === 0) { 
         output.push(number_names2[tens - 2]); 
        } else { 
         output.push(number_names2[tens - 2] + " " + GetWord(number)); 
        } 
       } 
      } 
      if (output.length > 2) { 
       for (var x = 0, limit = output.length - 1; x < limit; ++x) { 
        output[x] += "," 
       } 
      } 
      if (output.length > 1) { 
       var ubound = output.length - 1; 
       output[output.length] = output[ubound]; 
       output[ubound] = "and"; 
      } 
      if (is_negative) { 
       output.splice(0, 0, "negative"); 
      } 
      return output; 
     })(number).join(" "); 
    }; 
    var number_names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]; 
    var number_names2 = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"] 
    var count_names = ["hundred", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion"]; 
    var counts = [100]; 
    for (var x = counts.length, limit = count_names.length; x < limit; ++x) { 
     counts.push(Math.pow(1000, x)); 
    } 
})(); 

Cách sử dụng:

GetWord(1210); cho one thousand, two hundred, and ten

GetWord(2147483647); cho two billion, one hundred and forty seven million, four hundred and eighty three thousand, six hundred, and forty seven

GetWord(-123876124); cho negative one hundred and twenty three million, eight hundred and seventy six thousand, one hundred, and twenty four

Nếu bạn cần mũ, chỉ cần thay đổi các mẫu mảng đó thành biểu mẫu "giới hạn".

Các vấn đề liên quan