cosmic_sniper Posted April 23, 2012 Share Posted April 23, 2012 Aside from money_format(), is there another way to use "()" instead of "-" for negative numbers? Actually, using money_format() would be best for me but I've read somewhere that list of locale differ from server to server. Unfortunately, I cannot identify the list of locale supported on the server I'm using (Heliohost) since system('locale -a') is disabled. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/261498-using-for-negative-numbers/ Share on other sites More sharing options...
xyph Posted April 23, 2012 Share Posted April 23, 2012 It should be quite easy, but keep in mind adding brackets to an integer will convert it to a string, and be unusable with math operators. Check if the value is less than zero, if it is, multiply it by -1 (or convert to string and strip the leading hyphen) and surround it in brackets. <?php $int = -500; if( $int < 0 ) { $int *= -1; echo "($int)"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261498-using-for-negative-numbers/#findComment-1339943 Share on other sites More sharing options...
Andy-H Posted April 23, 2012 Share Posted April 23, 2012 function invert($number) { return (~$number + 1); } Quote Link to comment https://forums.phpfreaks.com/topic/261498-using-for-negative-numbers/#findComment-1339970 Share on other sites More sharing options...
cosmic_sniper Posted April 23, 2012 Author Share Posted April 23, 2012 Thanks it worked! Though, I made a little tweaking in able to incorporate number_format() that I need. <?php if($CG < 0) { $CG *= -1; echo "(". number_format($CG, 2, '.', ','). ")"; } else { echo number_format($CG, 2, '.', ','); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261498-using-for-negative-numbers/#findComment-1339984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.