JavaScript converts numbers into currency format strings _javascript Tips

Source: Internet
Author: User
Tags abs string format

The first way to do this is to use JavaScript to convert number numbers to a currency string format (parameters: Keep decimal digits, currency symbols, integer decimal separator, decimal separator)

The second method here is to convert a currency character to a pure numeric string with a simple regular expression, after which you can convert a string to a number
JavaScript Money Format (extended with prototype)

Extend the default number object with a Formatmoney () method:
//Usage:someVar.formatMoney (decimalPlaces, Symbol, ThousandsSeparator, DecimalSeparator)
//Defaults: (2, "$", ",", ".")
Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
  places =!isnan (places = Math.Abs (place s))? Places:2;
  Symbol = Symbol!== undefined? Symbol: "$";
  thousand = thousand | | ",";
  decimal = Decimal | | ".";
  var number = This,
    negative = number < 0? "-": "",
    i = parseint (number = Math.Abs (+number | | 0). ToFixed (places) + "",
    j = (j = i.length) > 3? J% 3:0;
  return symbol + negative + (J. I.substr (0, J) + Thousand: "") + I.substr (j). Replace (/(\d{3}) (? =\d)/g, "$" + thousand) + (Places. Decimal + math.abs (number-i). ToFixed (places). Slice (2): "");

Here are some examples of conversions:

Default usage and custom precision/symbol:
var revenue = 12345678;
Alert (Revenue.formatmoney ()); $12,345,678.00
alert (Revenue.formatmoney (0, "hk$")); hk$ 12,345,678

//European formatting:
var price = 4999.99;
Alert (Price.formatmoney (2, "?", ".", ",")); ? 4.999,99

//It works for negative values, too:
alert (( -500000). Formatmoney (0, "£")); £-500,000

Currency to number–removing money formatting (filtered with regular expressions)

var price = (12345.99). Formatmoney (); "$12,345.99"

//Remove non-numeric chars (except decimal point/minus sign):
priceval = parsefloat ( Price.replace (/[^0-9-.) /g, ")); 12345.99

This method applies only to the decimal separator, "." pattern, if the decimal separator is "," then the regular expression is/[^0-9-,]/g

do not need to prototype to expand the number of the version:

"/to set it up as a global function:function Formatmoney (number, places, symbol, Thousan
  D, decimal) {number = number | | 0; Places =!isnan (places = Math.Abs (places))?
  Places:2; Symbol = Symbol!== undefined?
  Symbol: "$"; thousand = thousand | |
  ","; decimal = Decimal | |
  "."; var negative = number < 0? "-": "", I = parseint (number = Math.Abs (+number | | 0). ToFixed (places) + "", j = (j = i.length) > 3?
  J% 3:0;  return symbol + negative + (J. I.substr (0, J) + Thousand: "") + I.substr (j). Replace (/(\d{3}) (? =\d)/g, "$" + thousand) +

(Places. Decimal + math.abs (number-i). ToFixed (places). Slice (2): "");} To create it as a library Method:myLibrary.formatMoney = function (number, places, symbol, thousand, decimal) {/* a s above *///Example Usage:formatmoney (54321); $54,321 Mylibrary.formatmoney (12345, 0, "£"); £12,345 

This is the entire article, learn more about JavaScript syntax, you can see: "JavaScript Reference Tutorial," JavaScript Code style guide, and also hope that you support the cloud-Habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.