How to prefix a positive number with plus sign in PHP

Posted by user318466 on Stack Overflow See other posts from Stack Overflow or by user318466
Published on 2010-04-21T11:22:43Z Indexed on 2010/04/22 2:03 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

I need to design a function to return negative numbers unchanged but should add a + sign at the start of the number if its already no present.

Example:

Input     Output
----------------
+1         +1
1          +1
-1         -1

It will get only numeric input.

function formatNum($num)
{
# something here..perhaps a regex?
}

This function is going to be called several times in echo/print so the quicker the better.

© Stack Overflow or respective owner

Related posts about php

Related posts about regex