hello...
i have this situation, i want to convert the number format like example "350", become string format "three hundred and fifty"..
Is it anyone who has the library to convert the number like example above..
thanks...
6 4682
i think there is no.
you have to make your own function in JS.
I think you are able to do that ..if any problem with writing the code then come here, experts are here.
Hi
Just save the following snippet as js file and call this file in code. -
-
/* Author: Sandesh Karalkar
-
* Copyrights: Sandesh Karalkar, 2004
-
* SEE: http://in.geocities.com/skexz/
-
* ----------------------------------------------------------------------
-
* CURRENCY TO WORDS CONVERTOR (Indian version)
-
* ----------------------------------------------------------------------
-
* Compatible with IE5.5+ and Mozilla 1.0+
-
*/
-
-
-
function digitToWordConvertor(number)
-
{
-
try
-
{
-
var number = getRoundedAmount(number);
-
var result = convertCrores(number);
-
//dump("\n Number: "+ "["+number+"] " + convertCrores(number));
-
return result;
-
}
-
catch (ex)
-
{
-
//dump("\n digitToWordConvertor =>" + ex);
-
}
-
}
-
-
-
function convertCrores(number)
-
{
-
try
-
{
-
var result;
-
if (number <1000000000)
-
{
-
if (number/10000000 >= 1)
-
{
-
result = convertTens(number/10000000) + " Crores";
-
if (number < 100000000)
-
{
-
var crores = String(number)+" ";
-
crores = crores.slice(1,-1);
-
//////dump("\n Thousunds :" + crores);
-
result = result + convertLakhs(crores);
-
}else{
-
var crores = String(number)+" ";
-
crores = crores.slice(2,-1);
-
//////dump("\n Thousunds :" + crores);
-
result = result + convertLakhs(crores);
-
}
-
}else{
-
result = convertLakhs(number);
-
}
-
}else{
-
////dump("\n Number is bigger than 10000000.");
-
}
-
return String(result);
-
}
-
catch (ex)
-
{
-
//dump("\n convertcrores =>" + ex);
-
}
-
}
-
-
-
function convertLakhs(number)
-
{
-
try
-
{
-
var result;
-
if (number < 10000000)
-
{
-
if (number/100000 >= 1)
-
{
-
result = convertTens(number/100000) + " Lakhs";
-
if (number < 1000000)
-
{
-
var lakhs = String(number)+" ";
-
lakhs = lakhs.slice(1,-1);
-
//////dump("\n Thousunds :" + lakhs);
-
result = result + convertThousund(lakhs);
-
}else{
-
var lakhs = String(number)+" ";
-
lakhs = lakhs.slice(2,-1);
-
//////dump("\n Thousunds :" + lakhs);
-
result = result + convertThousund(lakhs);
-
}
-
}else{
-
result = convertThousund(number);
-
}
-
}
-
return String(result);
-
}
-
catch (ex)
-
{
-
//dump("\n convertLakhs =>" + ex);
-
}
-
}
-
-
function convertThousund(number)
-
{
-
try
-
{
-
var result;
-
if (number < 100000)
-
{
-
if (number/1000 >= 1)
-
{
-
result = convertTens(number/1000) + " Thousand";
-
if (number < 10000)
-
{
-
var thousunds = String(number)+" ";
-
thousunds = thousunds.slice(1,-1);
-
//////dump("\n Thousunds :" + thousunds);
-
result = result + convertHundred(thousunds);
-
}else{
-
var thousunds = String(number)+" ";
-
thousunds = thousunds.slice(2,-1);
-
//////dump("\n Thousunds :" + thousunds);
-
result = result + convertHundred(thousunds);
-
}
-
}else{
-
result = convertHundred(number);
-
}
-
}else{
-
////dump("\n Number is bigger than 100000.");
-
}
-
return String(result);
-
}
-
catch (ex)
-
{
-
//dump("\n convertThousund =>" + ex);
-
}
-
}
-
-
-
function convertHundred(number)
-
{
-
try
-
{
-
var result;
-
if (number < 1000)
-
{
-
if (number/100 >= 1)
-
{
-
result = convertDigit(number/100) + " Hundred";
-
var tens = String(number)+" ";
-
tens = tens.slice(1,-1);
-
result = result + convertTwoDigits(tens);
-
}else{
-
result = convertTwoDigits(number);
-
}
-
}else{
-
////dump("\n Number is bigger than 1000.");
-
}
-
return result;
-
}
-
catch (ex)
-
{
-
//dump("\n convertHundred =>" + ex);
-
}
-
}
-
-
-
function convertTwoDigits(number)
-
{
-
try
-
{
-
var result;
-
if (number < 100)
-
{
-
if (number >= 10)
-
{
-
result = convertTens(number) + " ";
-
var tens1 = String(number)+" ";
-
tens1 = tens1.slice(3,-1);
-
result = result + convertDecimals(tens1);
-
}else{
-
result = convertTens(number) + " ";
-
var tens2 = String(number)+" ";
-
tens2 = tens2.slice(2,-1);
-
result = result + convertDecimals(tens2);
-
}
-
}else{
-
////dump("\n Number is bigger than 99.");
-
}
-
return result;
-
}
-
catch (ex)
-
{
-
//dump("\n convertTwoDigits =>" + ex);
-
}
-
}
-
-
-
function convertTens(number)
-
{
-
try
-
{
-
var convertTensResult;
-
if (number < 100)
-
{
-
if (number >= 10)
-
{
-
if (number >= 10 && number <= 19)
-
{
-
if (number >= 10 && number < 11)
-
{
-
convertTensResult = " Ten";
-
}
-
else if (number >= 11 && number < 12)
-
{
-
convertTensResult = " Eleven";
-
}
-
else if (number >= 12 && number < 13)
-
{
-
convertTensResult = " Twelve";
-
}
-
else if (number >= 13 && number < 14)
-
{
-
convertTensResult = " Thirteen";
-
}
-
else if (number >= 14 && number < 15)
-
{
-
convertTensResult = " Fourteen";
-
}
-
else if (number >= 15 && number < 16)
-
{
-
convertTensResult = " Fifteen";
-
}
-
else if (number >= 16 && number < 17)
-
{
-
convertTensResult = " Sixteen";
-
}
-
else if (number >= 17 && number < 18)
-
{
-
convertTensResult = " Seventeen";
-
}
-
else if (number >= 18 && number < 19)
-
{
-
convertTensResult = " Eighteen";
-
}
-
else if (number >= 19 && number < 20)
-
{
-
convertTensResult = " Nineteen";
-
}
-
//////dump("\n convertTensResult: " + convertTensResult);
-
}
-
else
-
{
-
var digitAtTen;
-
digitAtTen = number/10;
-
-
if (digitAtTen >= 2 && digitAtTen < 3)
-
{
-
convertTensResult = " Twenty";
-
}
-
else if (digitAtTen >= 3 && digitAtTen < 4)
-
{
-
convertTensResult = " Thirty";
-
}
-
else if (digitAtTen >= 4 && digitAtTen < 5)
-
{
-
convertTensResult = " Forty";
-
}
-
else if (digitAtTen >= 5 && digitAtTen < 6)
-
{
-
convertTensResult = " Fifty";
-
}
-
else if (digitAtTen >= 6 && digitAtTen < 7)
-
{
-
convertTensResult = " Sixty";
-
}
-
else if (digitAtTen >= 7 && digitAtTen < 8)
-
{
-
convertTensResult = " Seventy";
-
}
-
else if (digitAtTen >= 8 && digitAtTen < 9)
-
{
-
convertTensResult = " Eighty";
-
}
-
else if (digitAtTen >= 9)
-
{
-
convertTensResult = " Ninety";
-
}
-
else if (digitAtTen >= 0)
-
{
-
convertTensResult = "";
-
}
-
convertTensResult = convertTensResult +""+ convertDigit(String(number).charAt(1));
-
}
-
}else{
-
convertTensResult = convertDigit(number);
-
}
-
}else{
-
////dump("\n [ERROR:convertTens] The number provided is greater than 100.");
-
//convertTensResult = convertDigit(number);
-
}
-
return convertTensResult;
-
}
-
catch (ex)
-
{
-
//dump("\n convertTens =>" + ex);
-
}
-
}
-
-
-
function convertDigit(number)
-
{
-
try
-
{
-
var convertDigitResult
-
if (number >= 0 && number < 1)
-
{
-
convertDigitResult = "";
-
}
-
else if (number >= 1 && number < 2)
-
{
-
convertDigitResult = " One";
-
}
-
else if (number >= 2 && number < 3)
-
{
-
convertDigitResult = " Two";
-
}
-
else if (number >= 3 && number < 4)
-
{
-
convertDigitResult = " Three";
-
}
-
else if (number >= 4 && number < 5)
-
{
-
convertDigitResult = " Four";
-
}
-
else if (number >= 5 && number < 6)
-
{
-
convertDigitResult = " Five";
-
}
-
else if (number >= 6 && number < 7)
-
{
-
convertDigitResult = " Six";
-
}
-
else if (number >= 7 && number < 8)
-
{
-
convertDigitResult = " Seven";
-
}
-
else if (number >= 8 && number < 9)
-
{
-
convertDigitResult = " Eight";
-
}
-
else if (number >= 9 && number < 20)
-
{
-
convertDigitResult = " Nine";
-
}
-
return convertDigitResult;
-
}
-
catch (ex)
-
{
-
//dump("\n convertDigit =>" + ex);
-
}
-
}
-
-
function convertDecimals(number)
-
{
-
try
-
{
-
var result;
-
if (number != '00')
-
{
-
////dump("\n decimals: " + number);
-
//result = "Rupees "+ convertTens(number) +" Paisa only";
-
result="Rupees Only"
-
}else{
-
result = "Rupees Only";
-
}
-
return result;
-
}
-
catch (ex)
-
{
-
//dump("\n =>" + ex);
-
}
-
}
-
-
-
-
//Convert and round a number to two decimals
-
function getRoundedAmount(original_number)
-
{
-
var decimals = 2;
-
var result1 = original_number * Math.pow(10, decimals)
-
var result2 = Math.round(result1)
-
var result3 = result2 / Math.pow(10, decimals)
-
return pad_with_zeros(result3, decimals)
-
}
-
function pad_with_zeros(rounded_value, decimal_places) {
-
-
// Convert the number to a string
-
var value_string = rounded_value.toString()
-
-
// Locate the decimal point
-
var decimal_location = value_string.indexOf(".")
-
-
// Is there a decimal point?
-
if (decimal_location == -1) {
-
-
// If no, then all decimal places will be padded with 0s
-
decimal_part_length = 0
-
-
// If decimal_places is greater than zero, tack on a decimal point
-
value_string += decimal_places > 0 ? "." : ""
-
}
-
else {
-
-
// If yes, then only the extra decimal places will be padded with 0s
-
decimal_part_length = value_string.length - decimal_location - 1
-
}
-
-
// Calculate the number of decimal places that need to be padded with 0s
-
var pad_total = decimal_places - decimal_part_length
-
-
if (pad_total > 0) {
-
-
// Pad the string with 0s
-
for (var counter = 1; counter <= pad_total; counter++)
-
value_string += "0"
-
}
-
return value_string
-
}
-
You may find this useful.
Hi
Just save the following snippet as js file and call this file in code.
There's too much repetition and unnecessarily long code which can be cut down in size by some margin.
Ths is another approach- most of the code is for element creation and display, but you can use the 'num' methods with any library-
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title> number string test page</title>
<style type="text/css" media="screen">
html,body{background-color:#ffdead}
body{color:black;font-size:125%;font-family:'Times New Roman',serif}
#appDiv{width:75%;max-width:1000px; margin 1em auto}
#numDiv{position:relative;border:5px red ridge;padding:1ex 1em}
button,label, input, textarea{margin-right:5px;font-size:1.1em;font-weight:bold;line-height:1.2}
input,textarea{background:none white;}
button{cursor:pointer}
</style>
<script type="text/javascript">
// in practice, take the script out of the html
(function(){
var di= document.implementation;
if(!di || !di.hasFeature || !di.hasFeature('html','1.0')) return;
window.Run={
emSize: function(){
var who= Run.newElement('span',document.body,'','M',
{fontSize:'1em',padding:'0',position:'absolute',li neHeight:'1',visibility:'hidden'});
var fs= [who.offsetWidth,who.offsetHeight];
document.body.removeChild(who);
return fs;
},
mr: function(hoo){
if(!hoo) return false;
if(typeof hoo== 'string'){
hoo= document.getElementById(hoo);
if (!hoo) return false;
}
var t= hoo.nodeType;
if(t== 11 || t== 1)return hoo;
return false;
},
newElement:function(wot,pa,hoo,txt,sty,attr){
var who=Run.mr(hoo);
pa=Run.mr(pa);
if(who)who.parentNode.removeChild(who);
if(!wot)return;
sty=sty||{};
attr=attr||{};
var el=document.createElement(wot);
if(hoo)el.id=hoo;
for(var p in sty){
el.style[p]=sty[p];
}
for(var p in attr){
try{
el[p]=attr[p];
}
catch(er){
el.setAttribute(p,attr[p]);
}
}
if(txt)el.appendChild(document.createTextNode(txt) );
if(pa)pa.appendChild(el);
return el;
},
numDemo: function(){
var newEl=Run.newElement;
Run.mr('shownumBtn').style.visibility='hidden';
var pa=newEl('div','appDiv','numDiv');
var pa2=newEl('p',pa);
var lab=newEl('label',pa2,'','Number: ');
var inp= newEl('input',lab,'numInp','','',{type:'text',size :'20'});
inp.onchange=function(){
var v=Run.mr('numInp').value;
var v1=v.numWords();
if(v1){
Run.mr('numText').value+='\n'+v+'= '+v1;
}
Run.mr('numInp').value='';
Run.mr('numInp').focus();
}
newEl('button',pa2,'convertBtn','Convert','',{type :'button'});
newEl('button',pa2,'clearnumBtn','Clear','',{type: 'button'});
newEl('button',pa2,'numcloseBtn','Close','',{type: 'button'});
Run.mr('numcloseBtn').onclick= function(){
Run.mr('shownumBtn').style.visibility='visible';
Run.zap('numDiv');
}
Run.mr('clearnumBtn').onclick= function(){
Run.mr('numText').value='';
}
var pa3=newEl('p',pa);
var wh=Run.winSize();
var fs=Run.emSize();
var w=Math.floor(wh[0]/fs[0]);
if(w<20)w=20;
var h=Math.floor(wh[1]/(fs[1]*2));
var el=newEl('textarea',pa3,'numText','','',{cols:w,ro ws:h});
Run.mr(pa).style.width= (el.offsetWidth*1.1)+'px';
},
winSize: function(){
if(window.innerWidth)return [window.innerWidth,window.innerHeight];
var B= document.body;
var D= document.documentElement;
D= (D.clientWidth)? D: B;
return [D.clientWidth,D.clientHeight];
},
numWords: function(s){
s= s ||'';
var yr= false,str= '';
var W= Run.wordstrings;
var d= this.replace(/,/g,'');
var sign= (d.charAt(0)== '-')? 'minus ': '';
if(sign)d= d.substring(1);
var currency=(d.charAt(0)=='$');
if(currency){
d=d.substring(1);
d=parseFloat(d).toFixed(2);
}
if(isNaN(d))return '';
if(parseInt(d,10)>1.0e15){
var X= (+d).toExponential().split(/e/i);
return X[0].numWords()+ ' times ten to the '+
X[1].numWords(' power');
}
var Num= (d).split('.');
var A= Num[0].split('');
var r= Num[1];
var L= A.length;
var ones= L? +A.pop(): 0;
str= W[1][ones];
if(L<= 1) str= str || 'zero';
str= ' '+str;
if(!currency && (W[5] || /^y/i.test(s))){
yr= !r && L== 4 && A[0]== '1';
if(/^y/i.test(s))s= '';
}
if(L > 1){
var tens= +A.pop();
if(tens== 0 && ones>0 && L>2)str= ' and '+str;
else if(tens== 1){
str= ' '+W[2][ones];
}
else{
str= ' '+W[3][tens]+str;
}
if(L>2){
if(L>2){
var Am= W[4].slice(0) ;
var x= Am.shift(),n= 0;
var h= +A.pop();
if(!s && tens && !/^ *and/.test(str)) str=' and '+str;
if(h>0){
if(yr){
if(!tens)str= ' '+x+str;
str= W[2][h]+str.replace(/ and/,'');
return str;
}
else str= ' '+ W[1][h]+x+str;
}
if(L>3){
while(A.length){
n= A.pop();
x= Am.shift();
while(A.length && n.length<3) n= A.pop()+''+n;
if(parseInt(n)){
if(x!= W[4][1] && /\W/.test(str) && !/^ *and/.test(str))str=', '+str;
str= n.numWords(x)+ str;
}
}
}
}
}
}
if(currency){
if(str && !/zero/.test(str)){
str+=' dollar';
if(!/^ *one *dollar/.test(str))str+='s';
}
else str='';
if(r!='00'){
if(str)str+=' and';
str+= r.numWords()+' cents';
}
return sign + (str || 'Zero');
}
if(r){
str= str || 'zero ';
r= r.split('');
r= r.doforAll(function(itm){
return W[1][+itm]+'' || 'zero'
})
return sign +str+' point '+r.join(' ');
}
if(s== ' power'){
str= str.nth();
}
str= str || 'zero';
return sign + str +s;
},
wordstrings:
['English',
['','one','two','three','four','five','six','seven' ,'eight','nine'],
['ten','eleven','twelve','thirteen','fourteen','fif teen','sixteen','seventeen','eighteen','nineteen'],
['','','twenty','thirty','forty','fifty','sixty','s eventy','eighty','ninety'],
[' hundred',' thousand',' million',' billion',' trillion'],
false],
zap:function(who){
who= Run.mr(who);
if(who)who.parentNode.removeChild(who);
}
}
String.prototype.clean= function(){
var str= this.replace(/(^\s+)|(\s+$)/g,'');
return str.replace(/ {2,}/g,' ');
}
String.prototype.nth= function(){
var str= ' '+this.clean();
var L= str.length-1;
if(L && str.charAt(L-1)=='1'){
return str+'th';
}
var suffx= str.charAt(str.length-1);
if(isNaN(suffx)){
if(suffx== 'y') return str.slice(0,-2)+'tieth';
else{
var ax= str.lastIndexOf(' ');
if(ax== -1)ax+= 1;
suffx= str.substring(ax).clean();
if(ax>0)str= str.substring(0,ax);
else str= '';
}
}
switch(suffx){
case 'one': return str+'first';
case '1': return str+'rst';
case 'two':return str+'second';
case '2':return str+'nd';
case 'three': return str+'third';
case '3': return str+'rd';
case 'five': return str+'fifth';
case 'twelve': return str+'twelfth';
default: return str+suffx+'th';
}
}
String.prototype.numWords= Run.numWords;
Number.prototype.numWords= function(n){
if(typeof n== 'number') var s= this.toPrecision(n);
else var s= this+'';
if(n=== 'y') return s.numWords('y');
return s.numWords();
}
Array.prototype.doforAll= function(fun,args){
var L= this.length,A= [], tem;
for(var i= 0; i< L; i++){
try{
if(args)tem= fun.apply(this[i],args);
else tem= fun(this[i]);
}
catch(er){
tem= null;
}
if(tem) A.push(tem);
}
return A;
}
Array.prototype.copy=function(){
var A= [];
var L= this.length;
for(var i= 0;i<L;i++){
A[i]= this[i];
}
return A;
}
window.onload=function(){
var el= Run.newElement('button',document.body,'shownumBtn' ,'Converter','',
{title:'Start number to string converter',onclick:Run.numDemo});
}
})()
</script>
</head>
<body>
<h1>Numbers to words</h1>
<div id="appDiv">
<p>You can use this script to convert any number to (English) words.
If you begin the number with a dollar sign ('$') it will return currency, otherwise,
decimals will be 'spelled out'. Commas in the input will be ignored.</p>
<p><span style="font-weight:bold">Really</span> large numbers are converted to exponential notation.</p>
<p>Converting words to numbers is the next step...</p>
</div>
</body>
</html>
[/HTML]
This is the relevant section of the code in my previous post- - String.prototype.numWords= function(s){
-
s= s || '';
-
var yr= false, str= '';
-
var W= Number.wordstrings;
-
var d= this.replace(/, */g, '');
-
var sign=(d.charAt(0)== '-')? 'minus ': '';
-
if(sign)d= d.substring(1);
-
var currency=(d.charAt(0)=='$');
-
if(currency){
-
d=d.substring(1);
-
d=parseFloat(d).toFixed(2);
-
}
-
if(isNaN(d))return '';
-
if(parseInt(d, 10)>1.0e15){
-
var X=(+d).toExponential().split(/e/i);
-
return X[0].numWords()+ ' times ten to the '+
-
X[1].numWords(' power');
-
}
-
var Num=(d).split('.');
-
var A= Num[0].split('');
-
var r= Num[1];
-
var L= A.length;
-
var ones= L? +A.pop(): 0;
-
str= W[1][ones];
-
if(L<= 1) str= str || 'zero';
-
str= ' '+str;
-
if(!currency &&(W[5] || /^y/i.test(s))){
-
yr= !r && L== 4 && A[0]== '1';
-
if(/^y/i.test(s))s= '';
-
}
-
if(L > 1){
-
var tens= +A.pop();
-
if(tens== 0 && ones>0 && L>2)str= ' and '+str;
-
else if(tens== 1){
-
str= ' '+W[2][ones];
-
}
-
else{
-
str= ' '+W[3][tens]+str;
-
}
-
if(L>2){
-
if(L>2){
-
var Am= W[4].slice(0) ;
-
var x= Am.shift(), n= 0;
-
var h= +A.pop();
-
if(!s && tens && !/^ *and/.test(str)) str=' and '+str;
-
if(h>0){
-
if(yr){
-
if(!tens)str= ' '+x+str;
-
str= W[2][h]+str.replace(/ and/, '');
-
return str;
-
}
-
else str= ' '+ W[1][h]+x+str;
-
}
-
if(L>3){
-
while(A.length){
-
n= A.pop();
-
x= Am.shift();
-
while(A.length && n.length<3){
-
n= A.pop()+''+n;
-
}
-
if(parseInt(n)){
-
if(x!= W[4][1] && /\W/.test(str) &&
-
!/^ *and/.test(str)){
-
str=', '+str;
-
}
-
str= n.numWords(x)+ str;
-
}
-
}
-
}
-
}
-
}
-
}
-
if(currency){
-
if(str && !/zero/.test(str)){
-
str+=' dollar';
-
if(!/^ *one *dollar/.test(str))str+='s';
-
}
-
else str='';
-
if(r!='00'){
-
if(str)str+=' and';
-
str+= r.numWords()+' cents';
-
}
-
return sign +(str || 'Zero');
-
}
-
if(r){
-
str= str || 'zero ';
-
r= r.split('');
-
r= r.doforAll(function(itm){
-
return W[1][+itm]+'' || 'zero'
-
})
-
return sign +str+' point '+r.join(' ');
-
}
-
if(s== ' power'){
-
str= str.nth();
-
}
-
str= str || 'zero';
-
return sign + str +s;
-
}
-
Number.wordstrings=
-
[
-
'English',
-
['zero', 'one', 'two', 'three', 'four', 'five', 'six',
-
'seven' , 'eight', 'nine'],
-
['ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
-
'fifteen', 'sixteen',
-
'seventeen', 'eighteen', 'nineteen'],
-
['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
-
'seventy', 'eighty', 'ninety'],
-
[' hundred', ' thousand', ' million', ' billion', ' trillion'],
-
false
-
]
-
Number.prototype.numWords= function(n){
-
if(typeof n== 'number') var s= this.toPrecision(n);
-
else var s= this+'';
-
if(n=== 'y') return s.numWords('y');
-
return s.numWords();
-
}
-
Array.prototype.doforAll= function(fun, args){
-
var L= this.length, A= [], tem;
-
for(var i= 0; i< L; i++){
-
try{
-
if(args)tem= fun.apply(this[i], args);
-
else tem= fun(this[i]);
-
}
-
catch(er){
-
tem= null;
-
}
-
if(tem) A.push(tem);
-
}
-
return A;
-
}
-
/* these next methods are for getting the ordinal suffix(first, third, twelfth...) */
-
String.prototype.clean= function(){
-
var str= this.replace(/(^\s+)|(\s+$)/g,'');
-
return str.replace(/ {2,}/g,' ');
-
}
-
String.prototype.nth= function(){
-
var str= ' '+this.clean();
-
var L= str.length-1;
-
if(L && str.charAt(L-1)=='1'){
-
return str+'th';
-
}
-
var suffx= str.charAt(str.length-1);
-
if(isNaN(suffx)){
-
if(suffx== 'y') return str.slice(0, -2)+'tieth';
-
else{
-
var ax= str.lastIndexOf(' ');
-
if(ax== -1)ax+= 1;
-
suffx= str.substring(ax).clean();
-
if(ax>0)str= str.substring(0, ax);
-
else str= '';
-
}
-
}
-
switch(suffx){
-
case 'one': return str+'first';
-
case '1': return str+'rst';
-
case 'two':return str+'second';
-
case '2':return str+'nd';
-
case 'three': return str+'third';
-
case '3': return str+'rd';
-
case 'five': return str+'fifth';
-
case 'twelve': return str+'twelfth';
-
default: return str+suffx+'th';
-
}
-
}
-
// test case
var s= '15,278,456,341';
var txt='Type any number of digits(optional decimal point):\nA leading dollar sign($) converts currency.';
while((s= prompt(txt, s)) !=undefined){
txt=s+'= '+s.numWords()+'\n\nAnother?'
}
If you copy this script you can remove the line numbers from the returned string by pasting it in a textarea and running this-
textareareference.value=
textareareference.value.replace(/^\s*(\#?( *\d+) *\.)/gm,'');
Then you can paste the result in a webpage between script tags or just call eval(textareareference.value) to run it from the current window.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steven T. Hatton |
last post by:
I know I can write one, or use QString, etc., but is there a native C++
function to convert a NTBS of digits to an integer?
--
"If our hypothesis is about anything and not about some one or more...
|
by: whatluo |
last post by:
Hi, all
I'm now working on a program which will convert dec number to
hex and oct and bin respectively, I've checked the clc but with no
luck, so can anybody give me a hit how to make this done...
|
by: Chua Wen Ching |
last post by:
I have a problem. But on .NET 1.1
My Scenario:
Actually I will have a string of hexadecimals read from a xml file. Then
from the hexadecimals, i will add 1 value whenever i made any...
|
by: mwt |
last post by:
So in a further attempt to learn some Python, I've taken the little
Library program
(http://groups.google.com/group/comp.lang.python/browse_thread/thread/f6a9ccf1bc136f84)
I wrote and added...
|
by: ceeques |
last post by:
Hi
I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for...
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
|
by: JosAH |
last post by:
Greetings,
welcome back; above we discussed the peripherals of the Library class: loading
and saving such an instantiation of it, the BookMark interface and then some.
This part of the article...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |