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"
?>//  ww w.j  a  va 2  s  .c  o  m

Result

Related Topic