Convert numeric value to javascript string

Asked

Viewed 1,119 times

0

I am creating a receipt layout and I need the value of the receipt to be detailed in full in the body of the receipt, for example:

Transform R$ 45,51 in Forty-five reais and fifty-one cents.

Is there a library that does this, and if there isn’t, any hint of how to do it in the nail??

  • 1

    here is a package in npm: https://www.npmjs.com/package/extenso

  • boy... I saw something like that a long time ago, see if that’s it: http://jsfromhell.com/pt/string/extenso

  • the @Marcelorafael solution is better! :)

  • @Marcelorafael, this npm package solves my problem, thank you so much for pointing him out to me, and I thank the others for their help and collaboration...

1 answer

0


Javascript does not have a native function to perform this process.

However, you can use this function to return an extended number:

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/string/extenso [rev. #3]
String.prototype.extenso = function(c){
    var ex = [
        ["zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"],
        ["dez", "vinte", "trinta", "quarenta", "cinqüenta", "sessenta", "setenta", "oitenta", "noventa"],
        ["cem", "cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"],
        ["mil", "milhão", "bilhão", "trilhão", "quadrilhão", "quintilhão", "sextilhão", "setilhão", "octilhão", "nonilhão", "decilhão", "undecilhão", "dodecilhão", "tredecilhão", "quatrodecilhão", "quindecilhão", "sedecilhão", "septendecilhão", "octencilhão", "nonencilhão"]
    ];
    var a, n, v, i, n = this.replace(c ? /[^,\d]/g : /\D/g, "").split(","), e = " e ", $ = "real", d = "centavo", sl;
    for(var f = n.length - 1, l, j = -1, r = [], s = [], t = ""; ++j <= f; s = []){
        j && (n[j] = (("." + n[j]) * 1).toFixed(2).slice(2));
        if(!(a = (v = n[j]).slice((l = v.length) % 3).match(/\d{3}/g), v = l % 3 ? [v.slice(0, l % 3)] : [], v = a ? v.concat(a) : v).length) continue;
        for(a = -1, l = v.length; ++a < l; t = ""){
            if(!(i = v[a] * 1)) continue;
            i % 100 < 20 && (t += ex[0][i % 100]) ||
            i % 100 + 1 && (t += ex[1][(i % 100 / 10 >> 0) - 1] + (i % 10 ? e + ex[0][i % 10] : ""));
            s.push((i < 100 ? t : !(i % 100) ? ex[2][i == 100 ? 0 : i / 100 >> 0] : (ex[2][i / 100 >> 0] + e + t)) +
            ((t = l - a - 2) > -1 ? " " + (i > 1 && t > 0 ? ex[3][t].replace("ão", "ões") : ex[3][t]) : ""));
        }
        a = ((sl = s.length) > 1 ? (a = s.pop(), s.join(" ") + e + a) : s.join("") || ((!j && (n[j + 1] * 1 > 0) || r.length) ? "" : ex[0][0]));
        a && r.push(a + (c ? (" " + (v.join("") * 1 > 1 ? j ? d + "s" : (/0{6,}$/.test(n[0]) ? "de " : "") + $.replace("l", "is") : j ? d : $)) : ""));
    }
    return r.join(e);
}

She uses String.prototype so adds a method applied to all strings. For example:

"123".extenso();

How to use it you can see here Reference:

Jsfromhell
response of: link

  • He copied the same reply of the duplicate. A rather dishonest practice.

  • I signaled it was duplicated along with you @dvd, always do this. since those who asked here did not find the duplicate before or maybe did not understand, since the question will be closed even I see no problem in answering it to help those who asked, only that I should have put the answer link even this time I didn’t notice and it looked like a very ugly plagiarism. Kind of dishonest ? Like you said "half" is because you doubt whether or not it’s wrong. I also have , but as I said before, it may be that those who asked did not understand the answer in the question already asked on the site

  • @dvd but usually I change the answer is that this did not give even time. Not to stay 100% equal

  • I put "medium" to soften a little the content of the sentence, but since you suppose that I had some doubt, I rectify: I think totally dishonest. rs

  • @dvd even putting the link from where I took?

  • Yes. The correct one was to mark as duplicate (blz, you said you did it) and not answer; or you could even answer if it was another answer and not a faithful copy.

  • The right thing to do in this case would be to give a response from a colleague who has already been given an equal question.

  • But don’t worry about it. I’m not questioning your honesty. I know the intention was the best. Abs!

  • @dvd I do not know how it works if the question marked as duplicate is closed and does not appear in the searches giving opportunity to "honor" the question asked first hand, if it works like this I will keep the answer, if I do not think I should take, anyway in the next already know what to do :), ok obg

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.