PHP - String Format Number Signs

Introduction

By default, printf() displays negative numbers with a minus (-) symbol in front of them.

By default it doesn't put a plus (+) symbol in front of positive numbers.

To always display a sign symbol, use the sign specifier, +, in front of the type specifier.

Here's an example:

Demo

<?php

printf("%d \n ", 123 );   // Displays "123"
printf("%d \n ", -123 );  // Displays "-123"
printf("%+d \n ", 123 );  // Displays "+123"
printf("%+d \n ", -123 ); // Displays "-123"
?>//from   w ww  .  j  a  v a2 s  .co m

Result

Related Topic