Numbers written in Text format

SambaPOS has a Printer Tag for spelling out the Ticket Total as such:

{TOTALTEXT}
or
{TOTAL TEXT}

The above produces this:

So {TOTALTEXT} isn’t quite correct with the text all squeezed together. And neither of the above Tags are any good unless you require English and your currency is Dollars.

Add to that, if you want to output text for other numbers other than the Ticket Total, we have no method to do so.


I needed the same thing, but my requirement was for Spanish number-to-text. Using JScript in V5 we can implement this functionality for other languages, and we can apply it to any number we want, not just the Ticket Total.

I found this script (sorry to the author, it was a while ago and don’t remember where it came from), and it seems to work well for Spanish numbers …

##NumFunctions [num] (Script)##

Script Name: NumFunctions
Script Handler: num

Script:

var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve", "veinte", "veintiún", "veintidós", "veintitrés", "veinticuatro", "veinticinco", "veintiséis", "veintisiete", "veintiocho", "veintinueve"];
var decenas = ["", "", "", "treinta y ", "cuarenta y ", "cincuenta y ", "sesenta y ", "setenta y ", "ochenta y ", "noventa y "];
var centenas = ["", "ciento ", "doscient@s ", "trescient@s ", "cuatrocient@s ", "quinient@s ", "seiscient@s ", "setecient@s ", "ochocient@s ", "novecient@s "]
var ordenes = ["", " mil", " millones", " mil", " billones", " mil", " trillones", " mil", " cuatrillones", " mil"];
var gen = "num";
var digits = "0";

function numToSpanish(numToConvert) {
    var numeral = "";

    var numParts = chop(new String(numToConvert));

    for (i = 0; i < numParts.length; i++)
        numerator(numParts, i);

    numeral += numParts.reverse().join(" ");

    if (numeral.length == 0)
        numeral = "cero";

    if (/((llón)|(llones))\s*$/.test(numeral) && gen != "num")
        numeral += " de";

    if (gen == "fem")
        numeral += (digits == "1 " ? " libra" : " libras");
    else if (gen == "mas")
        numeral += (digits == "1 " ? " dólar" : " dólares");

    if (gen != "num")
        digits += (gen == "fem" ? "£" : "$");

    //return digits + "<br><br>" + numeral;
    numeral = numeral.toUpperCase();
    return numeral;
}

function numerator(numParts, pos) {
    var centena, decena, unidad, numeral;

    centena = numParts[pos].charAt(0);
    decena = numParts[pos].charAt(1);
    unidad = numParts[pos].charAt(2);

    if (parseInt(decena, 10) < 3)
        numeral = new String(centenas[centena] + unidades[parseInt(decena + unidad, 10)]);
    else
        numeral = new String(centenas[centena] + decenas[decena] + unidades[unidad]);

    switch (centena + decena + unidad) {
        case "000":
            if (ordenes[pos] != " mil" && numParts[pos + 1] != "000")
                numeral += ordenes[pos];
            break;
        case "001":
            numeral += ordenes[pos];
            numeral = numeral.replace(/^un\smil$/, "mil");
            if (pos == numParts.length - 1 || (pos < numParts.length - 1 && numParts[pos + 1] == "000"))
                numeral = numeral.replace(/llones$/, "llón");
            if (pos == 0 && gen != "mas")
                numeral = (gen == "fem" ? "una" : "uno");
            break;
        case "100":
            numeral = "cien " + ordenes[pos];
            break;
        default:
            numeral = numeral.replace(/@/g, ((gen == "fem" && pos < 2) ? "a" : "o"));
            numeral = numeral.replace(/\sy\s$/, "");
            numeral += ordenes[pos];
    }

    if (pos == 0 && gen != "mas") {
        numeral = numeral.replace(/veintiún$/, (gen == "fem" ? "veintiuna" : "veintiuno"));
        numeral = numeral.replace(/un$/, (gen == "fem" ? "una" : "uno"));
    } else if (pos == 1 && gen == "fem") {
        numeral = numeral.replace(/veintiún/, "veintiuna");
        numeral = numeral.replace(/un\smil/, "una mil");
    }
    numParts[pos] = numeral;
}

function chop(numString) {
    var numParts = new Array();
    var numPart = 0;
    var numDigits = 0;

    numString = numString.replace(/\D*/g, "").replace(/^0*/, "");
    if (numString == "")
        numString = "0";

    digits = "";
    for (var i = numString.length; i > 0; i -= 3) {
        numPart = numParts.push(numString.substring(i - 3, i));
        digits = numParts[numPart - 1] + " " + digits;
    }

    numDigits = numParts[numPart - 1].length;
    if (numDigits < 3)
        numParts[numPart - 1] = (numDigits == 2 ? "0" : "00") + numParts[numPart - 1];

    return numParts;
}


In the Printer Template we can now do a {CALL:X} to the script as follows:

{CALL:num.numToSpanish('{TICKET TOTAL}')}

I don’t need the decimal portion, so I actually call it like this, which trims the decimal point and last 2 digits (which are always 0 on my setup) …

{CALL:num.numToSpanish('{TICKET TOTAL}'.slice(0,-3))}

This is what I end up with on my printed Ticket:

7 Likes

If anyone finds similar scripts for other languages, please post them in this Topic! :slight_smile:

Hi @QMcKay

I am trying to implement this on my sales tickets (I guess for the same legal reasons you have to include it in Honduras). Is the above method still the suggested approach?

I am having a little difficulty manipulating the number before passing it to the script, and the script is way above me to work out where I could handle this in the script.

Some of my tickets have very odd ticket totals due to foreign currency rounding. I have 6 database decimal places, when I tried to take this back down to 2 I found it created issued with MXN rounding.

Please see the following ticket template, and resulting output…

<L00>{TICKET TOTAL}
<L00>[=F(TN("{TICKET TOTAL}"))]
<C00>{CALL:num.numToSpanish('{TICKET TOTAL}')} DÓLARES
<C00>{CALL:num.numToSpanish('[=F(TN("{TICKET TOTAL}"))]')} DÓLARES
29.99634409
30.00
DOS MIL NOVECIENTOS NOVENTA Y NUEVE MILLONES SEISCIENTOS TREINTA Y CUATRO MIL CUATROCIENTOS NUEVE DÓLARES
DOS MIL NOVECIENTOS NOVENTA Y NUEVE MILLONES SEISCIENTOS TREINTA Y CUATRO MIL CUATROCIENTOS NUEVE DÓLARES

I can’t work out how I can round and possibly .split() the number I am passing to the script. I am really confused why the 4th line still produces the same output, when above this syntax turns the number in to 30.00.

I should robably mention that the final output I am hoping to produce is:
TREINTA DÓLARES, 00/100 CENTAVOS

I am using the same method, but I made a small addition to the Script, and I call it as such:

<L00>{CALL:num.numToSpanish('{TICKET TOTAL}','{EXCHANGE RATE:HNL}')} Lempiras
<L00>{CALL:num.numToSpanish('{TICKET TOTAL}',1)} Dolares con {CALL:num.numToSpanish('{TICKET TOTAL}'.substr('{TICKET TOTAL}'.indexOf('.')+1)/1000,1)} Centavos

The calls to the function are passing in the Exchange Rate.

The 2nd line splits the number at the decimal point, and calls the function twice; once for each side of the decimal, so even the “centavos” are written. It does this by using substr and indexOf functions.

If you want to have cents as xx/100, then you can still do that by using the substr and indexOf functions, and wrap in all inside a Math.round function, then left-pad with zeros. This seems to work:

con [=('00' + (Math.round('{TICKET TOTAL}'.substr('{TICKET TOTAL}'.indexOf('.')+1)/10000)).toString()).slice(-2)]/100 Centavos

FYI, the /10000 above assumes 6 decimal places. We do this division for the round function. If we had 5 or 4 decimal places, it would be /1000 or /100 respectively.

You can see the formula is fairly long and ugly already. Now imagine you need to use the Exchange Rate in there… It might be best to script this, and pass in the Ticket Total and XR to a function in the “num” handler.

function cents(num,xr,dec) {
	dec = Number(dec);

	var div = Number(padR('1','0',dec-1)); // create our divider based on currency decimal places
	
	xr = Number(xr);
	
	num = Helper.ToNumber(num); // strip thousands separators
	num = num/xr;
	num = num.toString();

	num = num.substr(num.indexOf('.')+1); // get digits after decimal
	num = padR(num,'0',dec); // ensure the number is as long as our currency decimal places by padding right with zeros
	num = num/div;
	num = Math.round(num);
	num = num.toString();
	num = ('00' + num).slice(-2); // pad left with zeros
	
	return num;
}

function padR(str,padChar,padLen) {
    while (str.length < padLen)
        str = str + padChar;
    return str;
}

With that, we can do this in the Template; much cleaner:

con {CALL:num.cents('{TICKET TOTAL}','{EXCHANGE RATE:HNL}',6)}/100 Centavos

Or if we don’t need to apply XR, then just use 1 for that parameter:

con {CALL:num.cents('{TICKET TOTAL}',1,6)}/100 Centavos

We can even convert it to text:

<L00>con {CALL:num.cents('{TICKET TOTAL}','1',6)}/100 Centavos
<L00>con {CALL:num.numToSpanish("{CALL:num.cents('{TICKET TOTAL}','1',6)}",1)} Centavos

Assuming {TICKET TOTAL} is ‘5,444.123456’, the above produces:

con 12/100 Centavos
con DOCE Centavos

The main function now accepts the Exchange Rate, and converts the input into a Number (to remove thousands separators), then back to a string for processing. I did this so I wouldn’t need to make an ugly conversion to {TICKET TOTAL} inside the Template:

function numToSpanish(numToConvert,xr) {
	numToConvert = Helper.ToNumber(numToConvert)/Number(xr);
	//numToConvert = Math.round(numToConvert);
	numToConvert = parseInt(numToConvert);
	numToConvert = numToConvert.toString();

	
    var numeral = "";

With the new addition of the cents function, here is the full script:

//var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "diecis&eacute;is", "diecisiete", "dieciocho", "diecinueve", "veinte", "veinti&uacute;n", "veintid&oacute;s", "veintitr&eacute;s", "veinticuatro", "veinticinco", "veintis&eacute;is", "veintisiete", "veintiocho", "veintinueve"];
var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte", "veintiun", "veintidos", "veintitres", "veinticuatro", "veinticinco", "veintiseis", "veintisiete", "veintiocho", "veintinueve"];
var decenas = ["", "", "", "treinta y ", "cuarenta y ", "cincuenta y ", "sesenta y ", "setenta y ", "ochenta y ", "noventa y "];
var centenas = ["", "ciento ", "doscient@s ", "trescient@s ", "cuatrocient@s ", "quinient@s ", "seiscient@s ", "setecient@s ", "ochocient@s ", "novecient@s "]
var ordenes = ["", " mil", " millones", " mil", " billones", " mil", " trillones", " mil", " cuatrillones", " mil"];
var gen = "num";
//var gen = "fem";
var digits = "0";

function numToSpanish(numToConvert,xr) {
	numToConvert = Helper.ToNumber(numToConvert)/Number(xr);
	//numToConvert = Math.round(numToConvert);
	numToConvert = parseInt(numToConvert);
	numToConvert = numToConvert.toString();
	
    var numeral = "";

    var numParts = chop(new String(numToConvert));

    for (i = 0; i < numParts.length; i++)
        numerator(numParts, i);

    numeral += numParts.reverse().join(" ");

    if (numeral.length == 0)
        numeral = "cero";

    if (/((ll&oacute;n)|(llones))\s*$/.test(numeral) && gen != "num")
        numeral += " de";

    if (gen == "fem")
        numeral += (digits == "1 " ? " lempira" : " lempiras");
    else if (gen == "mas")
        numeral += (digits == "1 " ? " d&oacute;lar" : " d&oacute;lares");

    if (gen != "num")
        digits += (gen == "fem" ? "&pound;" : "$");

    //return digits + "<br><br>" + numeral;
    numeral = numeral.toUpperCase();
    return numeral;
}

function numerator(numParts, pos) {
    var centena, decena, unidad, numeral;

    centena = numParts[pos].charAt(0);
    decena = numParts[pos].charAt(1);
    unidad = numParts[pos].charAt(2);

    if (parseInt(decena, 10) < 3)
        numeral = new String(centenas[centena] + unidades[parseInt(decena + unidad, 10)]);
    else
        numeral = new String(centenas[centena] + decenas[decena] + unidades[unidad]);

    switch (centena + decena + unidad) {
        case "000":
            if (ordenes[pos] != " mil" && numParts[pos + 1] != "000")
                numeral += ordenes[pos];
            break;
        case "001":
            numeral += ordenes[pos];
            numeral = numeral.replace(/^un\smil$/, "mil");
            if (pos == numParts.length - 1 || (pos < numParts.length - 1 && numParts[pos + 1] == "000"))
                numeral = numeral.replace(/llones$/, "ll&oacute;n");
            if (pos == 0 && gen != "mas")
                numeral = (gen == "fem" ? "una" : "uno");
            break;
        case "100":
            numeral = "cien " + ordenes[pos];
            break;
        default:
            numeral = numeral.replace(/@/g, ((gen == "fem" && pos < 2) ? "a" : "o"));
            numeral = numeral.replace(/\sy\s$/, "");
            numeral += ordenes[pos];
    }

    if (pos == 0 && gen != "mas") {
        numeral = numeral.replace(/veinti&uacute;n$/, (gen == "fem" ? "veintiuna" : "veintiuno"));
        numeral = numeral.replace(/un$/, (gen == "fem" ? "una" : "uno"));
    } else if (pos == 1 && gen == "fem") {
        numeral = numeral.replace(/veinti&uacute;n/, "veintiuna");
        numeral = numeral.replace(/un\smil/, "una mil");
    }
    numParts[pos] = numeral;
}

function chop(numString) {
    var numParts = new Array();
    var numPart = 0;
    var numDigits = 0;

    numString = numString.replace(/\D*/g, "").replace(/^0*/, "");
    if (numString == "")
        numString = "0";

    digits = "";
    for (var i = numString.length; i > 0; i -= 3) {
        numPart = numParts.push(numString.substring(i - 3, i));
        digits = numParts[numPart - 1] + " " + digits;
    }

    numDigits = numParts[numPart - 1].length;
    if (numDigits < 3)
        numParts[numPart - 1] = (numDigits == 2 ? "0" : "00") + numParts[numPart - 1];

    return numParts;
}

function padR(str,padChar,padLen) {
    while (str.length < padLen)
        str = str + padChar;
    return str;
}

function cents(num,xr,dec) {
	dec = Number(dec);

	var div = Number(padR('1','0',dec-1)); // create our divider based on currency decimal places
	
	xr = Number(xr);
	
	num = Helper.ToNumber(num); // strip thousands separators
	num = num/xr;
	num = num.toString();

	num = num.substr(num.indexOf('.')+1); // get digits after decimal
	num = padR(num,'0',dec); // ensure the number is as long as our currency decimal places by padding right with zeros
	num = num/div;
	num = Math.round(num);
	num = num.toString();
	num = ('00' + num).slice(-2); // pad left with zeros
	
	return num;
}

Cheers @QMcKay

I have updated the script and the use of Math.round and indexOf gave me a couple of useful hints :slight_smile:

This is now working…

<C00>{CALL:num.numToSpanish(((Math.round({TICKET TOTAL}*100)/100).toFixed(2)).slice(0,-3),1)} DÓLARES
<C00>{CALL:num.numToSpanish(((Math.round({TICKET TOTAL}*100)/100).toFixed(2)).slice(-2),1)} CENTAVOS

Using .toFixed(2), we always end up with a number with two decimal places which can then be sliced. For example

  • 30.00
  • 30.25
  • 30.20

It saves a little bit of .substr and .indexOf

It seems to work well, the output is…

VEINTISIETE DÓLARES
SESENTA CENTAVOS
1 Like

Another function added to the script to do everything in one go using parameters:

<L00>{CALL:num.toSpanish('5,444.525456','1',6,'text')}
<L00>{CALL:num.toSpanish('5,444.525456',23,6)} -- 236.7184980869565

Produces:

CINCO MIL CUATROCIENTOS CUARENTA Y CUATRO Lempiras con CINCUENTA Y TRES Centavos
DOSCIENTOS TREINTA Y SEIS Dolares con 72/100

A regular call in the Template would be one of:

<L00>{CALL:num.toSpanish('{TICKET TOTAL}')}
<L00>{CALL:num.toSpanish('{TICKET TOTAL}',1)}
<L00>{CALL:num.toSpanish('{TICKET TOTAL}',1,6)}
<L00>{CALL:num.toSpanish('{TICKET TOTAL}',1,6,'text')}

<L00>{CALL:num.toSpanish('{TICKET TOTAL}','{EXCHANGE RATE:HNL}')}
<L00>{CALL:num.toSpanish('{TICKET TOTAL}','{EXCHANGE RATE:HNL}',6)}
<L00>{CALL:num.toSpanish('{TICKET TOTAL}','{EXCHANGE RATE:HNL}',6,'text')}

NOTE: the “currency words” are hard-coded in this function to 'Dolares' and 'Lempiras' in this line:
var currency = xr==1 ? 'Dolares' : 'Lempiras';

##toSpanish()##

function toSpanish(num,xr,dec,format) {
	xr = typeof(xr)==='undefined' || xr==''  ? 1 : xr;
	xr = Number(xr);

	dec = typeof(dec)==='undefined' || dec=='' ? 6 : dec;
	dec = Number(dec);

	format = typeof(format)==='undefined' || format=='' ? '' : format;

	var leftPart = numToSpanish(num,xr);
	var rightPart = cents(num,xr,dec);
	
	var currency = xr==1 ? 'Dolares' : 'Lempiras';
	
	var ret = '';
	
	switch (format) {
		case 'text':
			ret = leftPart + ' ' + currency + ' con ' + numToSpanish(rightPart,1) + ' Centavos';
			break;
		default:
			ret = leftPart + ' ' + currency + ' con ' + ('00' + rightPart).slice(-2) + '/100';
			break;
	}
	
	return ret;
}

NOTE: I added a lot more checks to all functions so we can skip parameters and it will use the following default values

xr = 1
dec = 6
format = ''

The functions are also arranged so that you can use any of them independently. For example, you can still call the cents() function without calling anything else.

##Full Script:
Name: NumFunctions
Handler: num

//var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "diecis&eacute;is", "diecisiete", "dieciocho", "diecinueve", "veinte", "veinti&uacute;n", "veintid&oacute;s", "veintitr&eacute;s", "veinticuatro", "veinticinco", "veintis&eacute;is", "veintisiete", "veintiocho", "veintinueve"];
var unidades = ["", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte", "veintiun", "veintidos", "veintitres", "veinticuatro", "veinticinco", "veintiseis", "veintisiete", "veintiocho", "veintinueve"];
var decenas = ["", "", "", "treinta y ", "cuarenta y ", "cincuenta y ", "sesenta y ", "setenta y ", "ochenta y ", "noventa y "];
var centenas = ["", "ciento ", "doscient@s ", "trescient@s ", "cuatrocient@s ", "quinient@s ", "seiscient@s ", "setecient@s ", "ochocient@s ", "novecient@s "]
var ordenes = ["", " mil", " millones", " mil", " billones", " mil", " trillones", " mil", " cuatrillones", " mil"];
var gen = "num";
//var gen = "fem";
var digits = "0";

function numToSpanish(numToConvert,xr) {
	xr = typeof(xr)==='undefined' || xr=='' ? 1 : xr;

	numToConvert = Helper.ToNumber(numToConvert)/Number(xr);
	//numToConvert = Math.round(numToConvert);
	numToConvert = parseInt(numToConvert);
	numToConvert = numToConvert.toString();
	
    var numeral = "";

    var numParts = chop(new String(numToConvert));

    for (i = 0; i < numParts.length; i++)
        numerator(numParts, i);

    numeral += numParts.reverse().join(" ");

    if (numeral.length == 0)
        numeral = "cero";

    if (/((ll&oacute;n)|(llones))\s*$/.test(numeral) && gen != "num")
        numeral += " de";

    if (gen == "fem")
        numeral += (digits == "1 " ? " lempira" : " lempiras");
    else if (gen == "mas")
        numeral += (digits == "1 " ? " d&oacute;lar" : " d&oacute;lares");

    if (gen != "num")
        digits += (gen == "fem" ? "&pound;" : "$");

    //return digits + "<br><br>" + numeral;
    numeral = numeral.toUpperCase();
    return numeral;
}

function numerator(numParts, pos) {
    var centena, decena, unidad, numeral;

    centena = numParts[pos].charAt(0);
    decena = numParts[pos].charAt(1);
    unidad = numParts[pos].charAt(2);

    if (parseInt(decena, 10) < 3)
        numeral = new String(centenas[centena] + unidades[parseInt(decena + unidad, 10)]);
    else
        numeral = new String(centenas[centena] + decenas[decena] + unidades[unidad]);

    switch (centena + decena + unidad) {
        case "000":
            if (ordenes[pos] != " mil" && numParts[pos + 1] != "000")
                numeral += ordenes[pos];
            break;
        case "001":
            numeral += ordenes[pos];
            numeral = numeral.replace(/^un\smil$/, "mil");
            if (pos == numParts.length - 1 || (pos < numParts.length - 1 && numParts[pos + 1] == "000"))
                numeral = numeral.replace(/llones$/, "ll&oacute;n");
            if (pos == 0 && gen != "mas")
                numeral = (gen == "fem" ? "una" : "uno");
            break;
        case "100":
            numeral = "cien " + ordenes[pos];
            break;
        default:
            numeral = numeral.replace(/@/g, ((gen == "fem" && pos < 2) ? "a" : "o"));
            numeral = numeral.replace(/\sy\s$/, "");
            numeral += ordenes[pos];
    }

    if (pos == 0 && gen != "mas") {
        numeral = numeral.replace(/veinti&uacute;n$/, (gen == "fem" ? "veintiuna" : "veintiuno"));
        numeral = numeral.replace(/un$/, (gen == "fem" ? "una" : "uno"));
    } else if (pos == 1 && gen == "fem") {
        numeral = numeral.replace(/veinti&uacute;n/, "veintiuna");
        numeral = numeral.replace(/un\smil/, "una mil");
    }
    numParts[pos] = numeral;
}

function chop(numString) {
    var numParts = new Array();
    var numPart = 0;
    var numDigits = 0;

    numString = numString.replace(/\D*/g, "").replace(/^0*/, "");
    if (numString == "")
        numString = "0";

    digits = "";
    for (var i = numString.length; i > 0; i -= 3) {
        numPart = numParts.push(numString.substring(i - 3, i));
        digits = numParts[numPart - 1] + " " + digits;
    }

    numDigits = numParts[numPart - 1].length;
    if (numDigits < 3)
        numParts[numPart - 1] = (numDigits == 2 ? "0" : "00") + numParts[numPart - 1];

    return numParts;
}

function padR(str,padChar,padLen) {
    while (str.length < padLen)
        str = str + padChar;
    return str;
}

function cents(num,xr,dec) {
	xr = typeof(xr)==='undefined' || xr==''  ? 1 : xr;
	xr = Number(xr);

	dec = typeof(dec)==='undefined' || dec=='' ? 6 : dec;
	dec = Number(dec);

	var div = Number(padR('1','0',dec-1)); // create our divider based on currency decimal places
		
	num = Helper.ToNumber(num); // strip thousands separators
	num = num/xr;
	num = num.toString();

	if (num.indexOf('.') > -1) {
		num = num.substr(num.indexOf('.')+1); // get digits after decimal
	} else {
		num = '0'; // there are no digits after decimal
	}
	
	num = num.substr(0,dec); // use only supplied decimal places
	num = padR(num,'0',dec); // ensure the number is as long as our currency decimal places by padding right with zeros
	num = num/div;
	num = Math.round(num);
	num = num.toString();
	//num = ('00' + num).slice(-2); // pad left with zeros
	
	return num;
}

function toSpanish(num,xr,dec,format) {
	xr = typeof(xr)==='undefined' || xr==''  ? 1 : xr;
	xr = Number(xr);

	dec = typeof(dec)==='undefined' || dec=='' ? 6 : dec;
	dec = Number(dec);

	format = typeof(format)==='undefined' || format=='' ? '' : format;

	var leftPart = numToSpanish(num,xr);
	var rightPart = cents(num,xr,dec);
	
	var currency = xr==1 ? 'Dolares' : 'Lempiras';
	
	var ret = '';
	
	switch (format) {
		case 'text':
			ret = leftPart + ' ' + currency + ' con ' + numToSpanish(rightPart,1) + ' Centavos';
			break;
		default:
			ret = leftPart + ' ' + currency + ' con ' + ('00' + rightPart).slice(-2) + '/100';
			break;
	}
	
	return ret;
}
1 Like