2015-05-14 66 views
-1

以下this教程我能夠將數字值轉換爲單詞。但我想改變貨幣價值不僅數字,所以我操縱它有點像這樣金錢價值字jquery

// 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 toWords(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 str1 = ''; //i added another word called cent 
    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 += 'and '; //i change the word point to and 
     str1 += 'cents '; //i added another word called cent 
     for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ' ; 
    } 
    //return str.replace(/\s+/g, ' '); 
    return str.replace(/\s+/g, ' ') + str1; //i added the word cent to the last part of the return value to get desired output 
} 

但我有我可以找出問題我自己的問題是如下

When i want to convert this money value `9.01` i will get this -> nine and zero one cents But what i want/need is to make it look like -> `nine and one cent` 
When i want to convert this money value `9.10` i will get this -> nine and one cents But what i want/need is to make it look like -> `nine and ten cents` 
When i want to convert this money value `9.11` i will get this -> nine and one one cents But what i want/need is to make it look like -> `nine and eleven cents` 
When i want to convert this money value `9.12` i will get this -> nine and one two cents But what i want/need is to make it look like -> `nine and twelve cents` 
+0

你有什麼問題嗎? 「但我有問題,我可以搞清楚自己」 – shershen

+0

這裏沒有jQuery。這是純粹的JS。 –

+0

你的問題在哪裏?和jQuery的地方?你能提供http://jsfiddle.net/ –

回答

1

看到以下工作代碼被剪掉。

// 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 toWords(s) { 
 

 
    s = s.toString(); 
 
    s = s.replace(/[\, ]/g, ''); 
 
    if (s != parseFloat(s)) return 'not a number'; 
 
    var x = s.indexOf('.'); 
 
\t var fulllength=s.length; 
 
\t 
 
    if (x == -1) x = s.length; 
 
    if (x > 15) return 'too big'; 
 
\t var startpos=fulllength-(fulllength-x-1); 
 
    var n = s.split(''); 
 
\t 
 
    var str = ''; 
 
    var str1 = ''; //i added another word called cent 
 
    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) { 
 
     
 
     str += 'and '; //i change the word point to and 
 
     str1 += 'cents '; //i added another word called cent 
 
     //for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ' ; 
 
\t \t var j=startpos; 
 
\t \t 
 
\t \t for (var i = j; i < fulllength; i++) { 
 
\t \t 
 
     if ((fulllength - 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] + ' '; 
 
\t \t \t \t 
 
       sk = 1; 
 
      } 
 
     } else if (n[i] != 0) { 
 
\t \t 
 
      str += dg[n[i]] + ' '; 
 
      if ((fulllength - i) % 3 == 0) str += 'hundred '; 
 
      sk = 1; 
 
     } 
 
     if ((fulllength - i) % 3 == 1) { 
 
\t \t 
 
      if (sk) str += th[(fulllength - i - 1)/3] + ' '; 
 
      sk = 0; 
 
     } 
 
    } 
 
    } 
 
\t var result=str.replace(/\s+/g, ' ') + str1; 
 
    //return str.replace(/\s+/g, ' '); 
 
\t $('.res').text(result); 
 
    return result; //i added the word cent to the last part of the return value to get desired output 
 
\t 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> 
 
<input type="text" class="convert"></input> 
 

 
<button onclick="toWords($('.convert').val())">Click me</button> 
 

 
<div class="res"></div>

+0

我需要澄清,當沒有美分。我需要擺脫'和CENTS'我找不到方法。 –