Skip to content

Instantly share code, notes, and snippets.

@peledies
Last active March 13, 2017 14:28
Show Gist options
  • Save peledies/4331eab66b599841e9bc9a456501354c to your computer and use it in GitHub Desktop.
Save peledies/4331eab66b599841e9bc9a456501354c to your computer and use it in GitHub Desktop.
Currency formatter for adding commas for silly humans
/****************************/
/* Author: Deac Karns
/* Date: 2017-03-10
/****************************/
// Useage:
// var num = 1234567.89
//
// console.log( num.currency() );
//
// output: 1,234,567.89
String.prototype.currency = function () {
return this.replace(/[^0-9.]/g, "")
.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
});
}
Number.prototype.currency = function () {
return String(this).currency();
}
public static function toCurrency($value)
{
$clear = preg_replace('/[^0-9.]/', '', $value);
return preg_replace('/\B(?=(\d{3})+(?!\d))/', ',', $clear);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment