/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Price with letters.

Ok. man..
this is a real challenge….
for me….
cause im not a serious coder….
but you are….!!!!

Ok..
this is to retrive numbers from a data base….
is a money format field….
now i have to write this number in other field… but..
in letters!!!!!

i remember to saw some simmilar script in other pages … but in fact i
don’t know where….

could anyone help me to find something to do this..
or if anyone has something simmilar… well… i will apreciate to
share it!!!!

thanks
Regards for all!!!!!
RT

to post a comment
JavaScript

26 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoJul 22.2006 — I am guessing that this is kinda what you are looking for:

[url=http://aorebirth.ao.funpic.org/testfiles/moneytest.html]Numbers to words[/url]

there are a few spacing errors that could be fixed, but nothing too significant.
Copy linkTweet thisAlerts:
@RterresauthorJul 22.2006 — well man..

you are the real thing!!!!!!!!

even better!!!!!

im going to check it....

and then i have to translate to spanish!!!!!!!

thanks

RT!
Copy linkTweet thisAlerts:
@JuuitchanJul 22.2006 — The program referenced by konithomimo contains bugs.

I might be able to write a better function myself.

What currency are you using, Rterres? Are hundredths of a unit used (like with dollars or euros), or not (like with yen)?

I will need your help with the Spanish. How do you want these amounts to look spelled out?


0.01 (if you use decimals)

1.01 (if you use decimals)

1,001,200

What about negative amounts? Will you use those? How do you spell those?
Copy linkTweet thisAlerts:
@konithomimoJul 22.2006 — The program referenced by konithomimo contains bugs.
[/QUOTE]


Which I said, but for a quick 5 minute program at 2 in the morning it isn't half bad. i could thnk of plenty of ways of improving it. I am merely trying to give the OP something to start with.
Copy linkTweet thisAlerts:
@JuuitchanJul 22.2006 — If you're going to give him something to start with, at least give him something worth starting with.
Copy linkTweet thisAlerts:
@JuuitchanJul 23.2006 — You require commas (for numbers like 1,000). The script doesn't say you require commas.

There are also other bugs.
Copy linkTweet thisAlerts:
@konithomimoJul 23.2006 — If you're going to give him something to start with, at least give him something worth starting with.[/QUOTE]
So says the person who seems to never post any useful contribution or working code that is worth using. As I said, it is a start. I could easily fix the bugs in it, as could most other people in this forum. You say that you could write better code, so go ahead and do it.
Copy linkTweet thisAlerts:
@konithomimoJul 23.2006 — Anyway, I took about 5 minutes to update the script to take care of most of the bugs. Here is what is fixed now:

  • - It will process zero correctly.

  • - All spacing issues

  • - The abscence of commas


  • The only things that you might want to add to it is to have it work when some commas are there, but others are not, and to capitalize the first letter of the display. Both of those are easy enough to fix, but I have to get going, so take care.
    Copy linkTweet thisAlerts:
    @RterresauthorJul 25.2006 — ok men..

    im back

    this is very well...

    i really admire your knowledge!!!!

    ok this script has buggies but well it helps..

    really thanks to both answering... then..

    ok..

    let me ask the juuitchan question...

    the currency is "pesos"

    0 this is "cero" sounds like english...

    0.01 (if you use decimals) this is cero "pesos con un centavo" (is like english to.. "cents"

    1.01 (if you use decimals) this is "un peso con un centavo"

    1,001,200 this is .. un millon un mil doscientos!

    thanks for aaaalll reading this..."!!!

    regards for all......

    RT
    Copy linkTweet thisAlerts:
    @konithomimoJul 25.2006 — Most of the bugs should have been fixed when I updated it a few days ago.
    Copy linkTweet thisAlerts:
    @RterresauthorJul 26.2006 — ok man..

    i appreciatte your knowledge and cooperation!!!!

    im going to check it!!!!

    and translate it!!

    Thanks!!!

    RT
    Copy linkTweet thisAlerts:
    @JuuitchanJul 26.2006 — You say that you could write better code, so go ahead and do it.[/QUOTE]

    <i>
    </i>function pesotext(amt) {

    var smallnums=new Array('cero ','un ','dos ','tres ','cuatro ',
    'cinco ','seis ','siete ','ocho ','nueve ',
    'diez ','once ','doce ','trece ','catorce ',
    'quince ','dieciseis ','diecisiete ','dieciocho ','diecinueve ',
    'veinte ','veintiún ','veintidós ','veintitrés ','veinticuatro ',
    'veinticinco ','veintiséis ','veintisiete ','veintiocho ',
    'veintinueve ');
    var tens=new Array('error','error','error','treinta ','cuarenta ',
    'cincuenta ','sesenta ','setenta ','ochenta ','noventa ');
    var ones=new Array('','y un ','y dos ','y tres ','y cuatro ',
    'y cinco ','y seis ','y siete ','y ocho ','y nueve ');
    var hundreds=new Array(' ','ciento ','doscientos ','trescientos ',
    'cuatrocientos ','quinientos ','seiscientos ','setecientos ',
    'ochocientos ','novecientos ');

    var work=Math.round(amt*100);
    var os='';

    if (work&lt;0) {os='menos '; work=-work;}
    if (work&gt;1e15) return 'error';

    var npart=new Array();
    var nfact=new Array(100,100,10,100,10,100,10,100);
    var p=0;
    for (p=0; p&lt;nfact.length; p++) {
    npart[p]=work%nfact[p];
    work=Math.round((work-npart[p])/nfact[p]) }

    if (npart[7]&lt;2) ;
    else if (npart[7]&lt;smallnums.length) os+=smallnums[npart[7]];
    else os+=(tens[Math.round((npart[7]-(npart[7]%10))/10)]
    +ones[npart[7]%10]);
    if (npart[7]&gt;0) os+='mil '

    if (npart[6]!=1 || npart[5]) os+=hundreds[npart[6]];
    else os+='cien ';
    if (npart[5]==0) ;
    else if (npart[5]&lt;smallnums.length) os+=smallnums[npart[5]];
    else os+=(tens[Math.round((npart[5]-(npart[5]%10))/10)]
    +ones[npart[5]%10]);
    if (npart[7] || npart[6]) os+='millones ';
    else if (npart[5]) os+=(npart[5]==1?'millón ':'millones ');

    if (npart[4]!=1 || npart[3]) os+=hundreds[npart[4]];
    else os+='cien ';
    if (npart[3]==0) ;
    else if (!(npart[7] || npart[6] || npart[5] || npart[4])
    &amp;&amp; npart[3]==1) ;
    else if (npart[3]&lt;smallnums.length) os+=smallnums[npart[3]];
    else os+=(tens[Math.round((npart[3]-(npart[3]%10))/10)]
    +ones[npart[3]%10]);
    if (npart[4] || npart[3]) os+='mil ';

    if (npart[2]!=1 || npart[1]) os+=hundreds[npart[2]];
    else os+='cien ';
    if (npart[1]==0) ;
    else if (npart[1]&lt;smallnums.length) os+=smallnums[npart[1]];
    else os+=(tens[Math.round((npart[1]-(npart[1]%10))/10)]
    +ones[npart[1]%10]);
    if ((npart[7] || npart[6] || npart[5]) &amp;&amp;
    !(npart[4] || npart[3] || npart[2] || npart[1]))
    os+='de ';
    if (npart[7] || npart[6] || npart[5]
    || npart[4] || npart[3] || npart[2]) os+='pesos';
    else if (npart[1]) os+=(npart[1]==1?'peso':'pesos');
    else os+='cero pesos';

    if (npart[0]) { os+=' con ';
    if (npart[0]&lt;smallnums.length) os+=smallnums[npart[0]];
    else os+=(tens[Math.round((npart[0]-(npart[0]%10))/10)]
    +ones[npart[0]%10]);
    os+=(npart[0]==1?'centavo':'centavos'); }

    return os; }

    Copy linkTweet thisAlerts:
    @RterresauthorJul 26.2006 — ok..

    im impressed!!!!

    this is a serious code....

    but im going to ask the las question!!!

    i show the number in a field.....this field makes a query...(some vbscript

    is involved) and shows some quantity... and then in other field..

    i have to show this same quantity in letters..!

    now the question.. how do i.. show this!!???

    Thanks again and many regards for all

    that read this!!!

    Rt.
    Copy linkTweet thisAlerts:
    @konithomimoJul 26.2006 — <i>
    </i>function pesotext(amt) {

    var smallnums=new Array('cero ','un ','dos ','tres ','cuatro ',
    'cinco ','seis ','siete ','ocho ','nueve ',
    'diez ','once ','doce ','trece ','catorce ',
    'quince ','dieciseis ','diecisiete ','dieciocho ','diecinueve ',
    'veinte ','veintiún ','veintidós ','veintitrés ','veinticuatro ',
    'veinticinco ','veintiséis ','veintisiete ','veintiocho ',
    'veintinueve ');
    var tens=new Array('error','error','error','treinta ','cuarenta ',
    'cincuenta ','sesenta ','setenta ','ochenta ','noventa ');
    var ones=new Array('','y un ','y dos ','y tres ','y cuatro ',
    'y cinco ','y seis ','y siete ','y ocho ','y nueve ');
    var hundreds=new Array(' ','ciento ','doscientos ','trescientos ',
    'cuatrocientos ','quinientos ','seiscientos ','setecientos ',
    'ochocientos ','novecientos ');

    var work=Math.round(amt*100);
    var os='';

    if (work&lt;0) {os='menos '; work=-work;}
    if (work&gt;1e15) return 'error';

    var npart=new Array();
    var nfact=new Array(100,100,10,100,10,100,10,100);
    var p=0;
    for (p=0; p&lt;nfact.length; p++) {
    npart[p]=work%nfact[p];
    work=Math.round((work-npart[p])/nfact[p]) }

    if (npart[7]&lt;2) ;
    else if (npart[7]&lt;smallnums.length) os+=smallnums[npart[7]];
    else os+=(tens[Math.round((npart[7]-(npart[7]%10))/10)]
    +ones[npart[7]%10]);
    if (npart[7]&gt;0) os+='mil '

    if (npart[6]!=1 || npart[5]) os+=hundreds[npart[6]];
    else os+='cien ';
    if (npart[5]==0) ;
    else if (npart[5]&lt;smallnums.length) os+=smallnums[npart[5]];
    else os+=(tens[Math.round((npart[5]-(npart[5]%10))/10)]
    +ones[npart[5]%10]);
    if (npart[7] || npart[6]) os+='millones ';
    else if (npart[5]) os+=(npart[5]==1?'millón ':'millones ');

    if (npart[4]!=1 || npart[3]) os+=hundreds[npart[4]];
    else os+='cien ';
    if (npart[3]==0) ;
    else if (!(npart[7] || npart[6] || npart[5] || npart[4])
    &amp;&amp; npart[3]==1) ;
    else if (npart[3]&lt;smallnums.length) os+=smallnums[npart[3]];
    else os+=(tens[Math.round((npart[3]-(npart[3]%10))/10)]
    +ones[npart[3]%10]);
    if (npart[4] || npart[3]) os+='mil ';

    if (npart[2]!=1 || npart[1]) os+=hundreds[npart[2]];
    else os+='cien ';
    if (npart[1]==0) ;
    else if (npart[1]&lt;smallnums.length) os+=smallnums[npart[1]];
    else os+=(tens[Math.round((npart[1]-(npart[1]%10))/10)]
    +ones[npart[1]%10]);
    if ((npart[7] || npart[6] || npart[5]) &amp;&amp;
    !(npart[4] || npart[3] || npart[2] || npart[1]))
    os+='de ';
    if (npart[7] || npart[6] || npart[5]
    || npart[4] || npart[3] || npart[2]) os+='pesos';
    else if (npart[1]) os+=(npart[1]==1?'peso':'pesos');
    else os+='cero pesos';

    if (npart[0]) { os+=' con ';
    if (npart[0]&lt;smallnums.length) os+=smallnums[npart[0]];
    else os+=(tens[Math.round((npart[0]-(npart[0]%10))/10)]
    +ones[npart[0]%10]);
    os+=(npart[0]==1?'centavo':'centavos'); }

    return os; }

    [/QUOTE]


    lets see, it took you nearly 5 days, and there are tons of bugs . . . YEAH!!! WAY BETTER CODE!!!

    Bugs:

    -code accepts input with or without quotations.

    -it doesnt handle commas correctly

    Send it 1,213 and it returns "un peso"

    Send it '1,213' and it returns "NaNundefinedNaNundefinedNaNundefinedNaNcero pesos"

    and there are many more . . . like you said, MUCH BETTER CODE!!!
    Copy linkTweet thisAlerts:
    @konithomimoJul 26.2006 — ok..

    im impressed!!!!

    this is a serious code....

    but im going to ask the las question!!!

    i show the number in a field.....this field makes a query...(some vbscript

    is involved) and shows some quantity... and then in other field..

    i have to show this same quantity in letters..!

    now the question.. how do i.. show this!!???

    Thanks again and many regards for all

    that read this!!!

    Rt.[/QUOTE]


    Simply send the quantity received to the function. For example, lets say you get the quantity from another function. Then you would just do:

    changeMoney(getValue());

    Where getValue() is the other function that you are calling.
    Copy linkTweet thisAlerts:
    @RterresauthorJul 26.2006 — ok men..

    please lets do this turn in a battle field!!!

    is a cooperative forum, isnt it?

    ok..

    then lets work in make it better...ok??

    thanks really... both are great coders..

    Regards

    RT.
    Copy linkTweet thisAlerts:
    @JuuitchanJul 27.2006 — [B]JavaScript itself[/B] does not accept numbers with commas. It uses commas as separators.

    If you have JavaScript evaluate "foo(12,345)", you are passing two arguments to foo(): the first argument is 12 and the second is 345. This is just JavaScript syntax at work.
    Copy linkTweet thisAlerts:
    @JuuitchanJul 27.2006 — Rterres:

    You mentioned VBScript.

    Does the function you need have to be in VBScript? If it does, you should have posted in the VBScript forum, not the JavaScript forum. Since you posted in the JavaScript forum, naturally you got JavaScript functions.


    konithomimo:

    First: Most likely, Rterres would not use a comma when writing numbers, except as a decimal point.

    Second: We don't know how numbers are treated by Rterres' database program. Most likely they are treated as numerics, rather than as strings.
    Copy linkTweet thisAlerts:
    @RterresauthorJul 27.2006 — ok but the problem is in the javascript number reader...

    the vb script query is part of the web application and it works..

    the problem is related with reading the number displayed in the field and write it down in other field with letters...

    now the commas problem is important, i did not specify something about this..

    the number that is retrived in the field... is already formated with $ 00,00.00

    the very first capture page in this application, has a javascript currency converter..

    when the user write some quantity.. 5600.. in a money related field.. this field turns automatically in $5,600.00. and in this format is captured in the database

    then.. i have to read it and write it... like that....

    is that a big problem??? =(

    as always..

    best regards...

    RT
    Copy linkTweet thisAlerts:
    @JuuitchanJul 27.2006 — It is a [B]huge[/B] problem, Rterres.

    Why did you not tell us exactly what it was you were trying to do?

    You said that you wanted a conversion, but you did not say what you were converting [B]from[/B].

    My function will only work if you pass it the amount either:

    (1) as a number

    or

    (2) as a string, without commas, [B] and without $ signs[/B]. I don't know if leading spaces will do any harm, but I wouldn't use them.

    I assume that your database program (or whatever it is that you use) has the ability to convert things like "$1,234.50" to numbers like 1234.5 -- it would probably need this in order to do arithmetic on amounts of money.

    If you use my function, Rterres, [B]please[/B] have your database program convert your money amount (like "$1.50") to a number (like 1.5) [B]before[/B] you send it to my function.
    Copy linkTweet thisAlerts:
    @slaughtersJul 27.2006 — *SIGH*

    No comments. Short obscure naming convention used for variables. Inconsistant indentation levels used for loops and if statements. Use of "magic numbers" sprinkled throughout the code, etc., etc..

    In other words all that I have learned to expect from todays crop of programmers. ?

    P.S. Sorry - did not actually look to see if the code worked, BUT don't Europeans use a decimal point instead of a comma to seperate their numbers every three digits? I seem to remember seeing that used somewhere.
    Copy linkTweet thisAlerts:
    @kuervoSep 11.2006 — I have a big problem with this script, when you write a number like "100", the result is "[COLOR=DarkRed]one hundredundefined dollar(s).[/COLOR]", and belive me this script is perfect for my proyect, but i can´t find the solution for this problem in the code. Thanks.

    Para los que hablan español, les pido por favor si alguien sabe el problema porqué me muestra esto: one hundredundefined dollar(s). cuando pongo 100 por ejemplo que coloque la respuesta aquí, ya que no se nada de código javascipt y la verdad no se como arreglar esto. Gracias.
    Copy linkTweet thisAlerts:
    @konithomimoSep 12.2006 — i will take a look at it in a bit and fix the ug. I fixed it once before, but since there was no response about the script i didnt update it.

    Ok, to fix it simply replace this:

    if(hto&lt;20){
    d+=ff[hto-1];
    return (d+tt[num]);
    }


    with this:

    if(hto&lt;20){
    [color=red]if(hto==0)
    return(d);[/color]
    d+=ff[hto-1];
    return (d+tt[num]);
    }


    Or just add the lines in red.
    Copy linkTweet thisAlerts:
    @kuervoSep 12.2006 — thanks!!! That work perfectly, im really appreciated your help.
    Copy linkTweet thisAlerts:
    @konithomimoSep 13.2006 — Glad to help
    Copy linkTweet thisAlerts:
    @konithomimoSep 13.2006 — *SIGH*

    No comments. Short obscure naming convention used for variables. Inconsistant indentation levels used for loops and if statements. Use of "magic numbers" sprinkled throughout the code, etc., etc..

    In other words all that I have learned to expect from todays crop of programmers. ?

    P.S. Sorry - did not actually look to see if the code worked, BUT don't Europeans use a decimal point instead of a comma to seperate their numbers every three digits? I seem to remember seeing that used somewhere.[/QUOTE]

    Yes, commas are used, but I merely used the currency format of my own country. As well, most numbers used are not randmomly chosen via a dartboard as you seem to think, they are simply just not commented. The reason being that most users do not care about that, and unless I am writing code for myself of for an extensive project I do not use high level of abstraction and proper programming etiquette, since for most cases it is pointless since the user wants the code for a specific case.
    ×

    Success!

    Help @Rterres 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.5,
    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,
    )...