Removing the space after the dollar sign in the output of PHP's money_format()
I use
money_format('%(#15.2n',$money)
开发者_如何学编程
It outputs something like
$ 500.00
Is there a way to remove the space between the dollar sign and 500?
money_format
[docs] may not even work on some platforms. It would be more reliable to use number_format
[docs]:
echo '$' . number_format($money, 2); // '$12.44'
If you wanted it padded to a certain number of spaces, but with the dollar sign immediately before the number, you could also use sprintf
[docs]:
echo printf("%15s", '$' . number_format($money, 2)); // ' $12.44'
use str_replace(" ", "", $string)
To remove the extra spaces is fairly simple. The code you're using is actually adding the padding. An example from the money_format
documentation:
// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($ 1,234.57)
The #15
in your example is the culprit. If you were to look at your reference inside of a pre
tag you would actually see the number padded to 15 spaces.
$ 500.00
If you remove that...
money_format('%(.2n',$money)
You will get:
$500.00
You can also drop the (
to not wrap the negative numbers in parentheses (the alternative is a -
at the beginning). And if you actually still want that and the single space you reference in your expected result you can try:
money_format('%(#4.2n',$money)
$str = preg_replace('/\s\s+/', ' ', $str);
This is what I use:
function my_money_format($number){
return str_replace(" ", "",money_format('%+#10n', $number));
}
echo money_format('$%i', $number)
精彩评论