Last active
March 13, 2017 14:28
-
-
Save peledies/4331eab66b599841e9bc9a456501354c to your computer and use it in GitHub Desktop.
Currency formatter for adding commas for silly humans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************/ | |
/* 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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