Javascript: Convert a number to written text and currency format functions.

If you ever have to write any check writing software, or something for the banking industry, you will most likely need to convert a number to a text-written string. For example… $100.25 becomes One Hundred and Twenty-Five Cents.

I had some code written in C which I converted to Javascript for a web page I was recently working on. I’ve also included a function for printing a number formatted as US currency regardless of whether the input is formatted or not.Example

Here’s a quick demo of the following function. You can enter a number such as 154934 or a dollar amount such as $123,342,234.43. The limit of this function is 999 Trillion, so our representatives should be safe to use this with their deficit for the next 4 years or so… 🙂

Here’s a javascript function that converts a number or a string representing a dollar amount to written text.

Example

Here’s a quick demo of the following function. You can enter a number such as 154934 or a dollar amount such as $123,342,234.43. The limit of this function is 999 Trillion, so our representatives should be safe to use this with their deficit for the next 4 years or so… 🙂

Here’s a javascript function that converts a number or a string representing a dollar amount to written text.

function nbr2txt(number)
{
if (typeof(number) == "string") {
var currency = number;
//convert if string, remove dollar sign, commas
number = currency.replace(/[^0-9\.]+/g,"");
}

var ones = new Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight","Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen");
var tens = new Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety");

var cents = number - (Math.floor(number));
cents = Math.round(cents * 100);
var nbr = Math.floor(number);

var tn = Math.floor(nbr / 1000000000000);
nbr -= tn * 1000000000000;
var bn = Math.floor(nbr / 1000000000);
nbr -= bn * 1000000000;
var gn = Math.floor(nbr / 1000000);
nbr -= gn * 1000000;
var kn = Math.floor(nbr / 1000);
nbr -= kn * 1000;
var hn = Math.floor(nbr / 100);
nbr -= hn * 100;
var dn = Math.floor(nbr / 10);
nbr -= dn * 10;
var n = nbr % 10;

var res = "";
if (tn) {
res += (res.length == 0 ? "" : " ") + nbr2txt(tn) + " Trillion";
}
if (bn) {
res += (res.length == 0 ? "" : " ") + nbr2txt(bn) + " Billion";
}
if (gn) {
res += (res.length == 0 ? "" : " ") + nbr2txt(gn) + " Million";
}
if (kn) {
res += (res.length == 0 ? "" : " ") + nbr2txt(kn) + " Thousand";
}
if (hn) {
res += (res.length == 0 ? "" : " ") + nbr2txt(hn) + " Hundred";
}

if (dn || n) {
if (res.length > 0) {
res += " ";
}
if (dn < 2) {
res += ones[dn * 10 + n];
}
else {
res += tens[dn];
if (n) {
res += "-" + ones[n];
}
}
}
if (cents) {
res += (res.length == 0 ? "" : " and ") + nbr2txt(cents) + " Cents";
}
if (res.length == 0) {
res = "Zero";
}

return res;
}

The following function converts a number or string to a US currency formatted string. I’ve written the function in a way that can be easily coded in a different language, such as PHP or C.

function nbr2currency(number)
{
var decimal_sep = ".";
var thousands_sep = ",";
var dollar_sign = "$";

if (typeof(number) == "string") {
var currency = number;
//convert if string
number = currency.replace(/[^0-9\.]+/g,"");
}

var cents = number - (Math.floor(number));
cents = Math.round(cents * 100);
var nbr = Math.floor(number) + "";

var output = "";
mylen = nbr.length;

for (x = 0; x < mylen; x++) {
if ((mylen - x) % 3 == 0) {
output += ((output.length == 0) ? "" : thousands_sep) + nbr[x];
}
else {
output += nbr[x];
}
}
output += (cents ? decimal_sep + cents : decimal_sep + "00");

return dollar_sign + " " + output;
}

If you like this code, or use it, please link to this page.

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*