/    Sign up×
Community /Pin to ProfileBookmark

As anyone seen this in Javascript

I wonder if any one can help me, I’ve been searching the net for a javascript script, which when you type in any figure it will return that figure in written words.

For example: 200.10 returns two hundred dollars and ten cents,

and : 1.1 = one dollar and one cent (note single and plural dollar/dollars and cent/cents)

I’ve found scripts in PHP & VB but as I know less about these then javascript, can I convert these to javascript (easy) ?

I don’t know where to start, I’m not that great with javascript but could modify and change a working script to fit my bill.

Many Thanks for any help or advice, and sorry if I posted in the wrong place

Steve

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@felgallDec 17.2007 — http://javascript.about.com/library/bltoword.htm converts numbers to words but doesn't handle the currency part of what you want so you'd have to add that part to it.
Copy linkTweet thisAlerts:
@steve_727authorDec 19.2007 — Many Thanks Stephen,

Will have a play with this script,

sorry for the delay in getting back been busy this end,

I've been forced Xmas shopping by wife, (for my wife) ?

I'm sure you're understand.

thanks again

Steve.
Copy linkTweet thisAlerts:
@mrhooDec 19.2007 — This one converts numbers to English words- if you preface the number with a dollar sign it returns 'dollars' and 'cents'.

You won't need all of it, but take anything you can use.
[CODE]<!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}
#numDiv{position:relative;border:5px red ridge;padding:1ex 1em}
button,label, input, textarea{margin-right:1em;font-size:1.1em;font-weight:bold;line-height:1.2}
input,textarea{background:none white}
p{width:600px;font-weight:500}
button{cursor:pointer}
</style>

<script type="text/javascript">
// in practice, take the script out of the html

(function(){
if(typeof Run!= 'object') Run={};
if(Run.numWords)return true;
var di= document.implementation;
if(!di || !di.hasFeature || !di.hasFeature('html','1.0'))return;
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';
}
}

Run.wordstrings=
['English',
['','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]
Run.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].copy() ;
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;
}
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;
}
String.prototype.clean= function(){
var str= this.replace(/(^s+)|(s+$)/g,'');
return str.replace(/ {2,}/g,' ');
}
Array.prototype.copy=function(){
var A= [];
var L= this.length;
for(var i= 0;i<L;i++){
A[i]= this[i];
}
return A;
}
//The rest of this is for the page display

Run.emSize= function(){
var who= Run.newElement('span',document.body,'','M',
{fontSize:'1em',padding:'0',position:'absolute',lineHeight:'1',visibility:'hidden'});
var fs= [who.offsetWidth,who.offsetHeight];
document.body.removeChild(who);
return fs;
}
Run.newElement=function(wot,pa,hoo,txt,sty,attr){
var who=mr(hoo);
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(mr(pa))mr(pa).appendChild(el);
return el;
}
Run.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];
}

Run.numDemo= function(){
var newEl=Run.newElement;
mr('shownumBtn').style.visibility='hidden';
var pa=newEl('div',document.body,'numDiv');
var pa2=newEl('p',pa);
var lab=newEl('label',pa2,'','Type a number and press "Enter":');
var inp= newEl('input',lab,'numInp','','',{type:'text',size:'20'});
inp.onchange=function(){
var v=mr('numInp').value;
var v1=v.numWords();
if(v1){
mr('numText').value+='n'+v+'= '+v1;
}
mr('numInp').value='';
mr('numInp').focus();
}
inp.onkeypress=function(e){
e= window.event? event: e;
var who=e.srcElement|| e.target;
if(e.keyCode== 13)return setTimeout(function(){who.blur()},50);
}
newEl('button',pa2,'clearnumBtn','Clear','',{type:'button'});
newEl('button',pa2,'numcloseBtn','Close','',{type:'button'});
mr('numcloseBtn').onclick= function(){
mr('shownumBtn').style.visibility='visible';
document.body.removeChild(mr('numDiv'))
}
mr('clearnumBtn').onclick= function(){
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,rows:h});
mr(pa).style.width= (el.offsetWidth*1.1)+'px';
}
window.mr= function mr(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;
}
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</h2>
<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 a number value is the next step...</p>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@steve_727authorDec 21.2007 — Thanks Mrhoo

This will do nicely, all info much appreciated ?

Steve.
×

Success!

Help @steve_727 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...