Jump to content

Using "()" for Negative Numbers


cosmic_sniper

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/261498-using-for-negative-numbers/
Share on other sites

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)";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.