Show signs on PHP numbers

PHP automatically shows a negative sign for negative numbers but getting it to show a positive sign (e.g. when you are showing growth/loss) can be a mess of if statements. Rather, use this —

sprintf("%+d", $number);

You’ll then get +1234 instead of just 1234.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s