Skip to content

Instantly share code, notes, and snippets.

@zfkun
Created May 30, 2014 05:55
Show Gist options
  • Save zfkun/337e72e5bdcd247dfaf2 to your computer and use it in GitHub Desktop.
Save zfkun/337e72e5bdcd247dfaf2 to your computer and use it in GitHub Desktop.
number to money style
define( function ( require, exports, module ) {
/**
* 货币数字格式化
* 将数字整数部分按3位分组加逗号格式化
*
* @example
* ```javascript
* var money = 123456789.1234567;
* var moneyFormated = toMoney( money );
* console.info( 'format from `%s` to `%s`', money, moneyFormated );
* // output: format from `123456789.1234567` to `123,456,789.1234567`
* ```
* @param {number|string} number 待格式化的数字
* @return {string} 格式化后的字符串
*/
function toMoney ( number, group ) {
return number.toString().replace( /\B(?=(\d{3})+(?:\.\d+$))/g, ',' );
}
return toMoney;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment