JS converts the number value into a currency format

Source: Internet
Author: User
ArticleDirectory
    • Here are some conversion instances:

JS converts the number value into a currency format:

Here, the first method is to use JavaScript to convert the number to the format of the currency string (parameter: retain decimal places, currency symbols, integer part Of the thousands separator, decimal separator)

Here, the second method is to convert the currency character to a pure numeric string using a simple regular expression, and then convert the string to a numeric number.

Javascript money format (extended by 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 (places ))? 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), 10) + "", 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, "$1" + thousand) + (places? Decimal + math. Abs (number-I). tofixed (places). Slice (2 ):"" );}; 

 

Here are some conversion instances:
     //  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, "€ ",".",",")); //  €0. 4.999, 99      //  It works for negative values, too: Alert (-500000). formatmoney (0, "£ ")); //  £-500,000 

 

Currency to number-removing money formatting (filter using regular expressions)
 
VaRPrice = (12345.99). formatmoney ();//"$12,345.99"//Remove non-numeric Chars (alias t decimal point/minus sign ):Priceval = parsefloat (price. Replace (/[^ 0-9-.]/g ,''));//12345.99

This method is only applicable to the mode where the decimal separator is ".". If the decimal separator is "," The regular expression is/[^ 0-,]/g

 

Versions extended by number without prototype:
     //  To set it up as a global function:      Function Formatmoney (number, places, symbol, thousand, 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), 10) + "" , 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, "$1" + 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 ){  /*  As abve  */  } //  Example usage: Formatmoney (54321 ); //  $54,321 Mylibrary. formatmoney (12345, 0, "£ "); //  £5. 12,345 

 

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.