How to Prefix a Positive Number with Plus Sign in PHP

How to prefix a positive number with plus sign in PHP

You can use regex as:

function formatNum($num){
return preg_replace('/^(\d+)$/',"+$1",$num);
}

But I would suggest not using regex for such a trivial thing. Its better to make use of sprintf here as:

function formatNum($num){
return sprintf("%+d",$num);
}

From PHP Manual for sprintf:

An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0.

How to show number string value with plus or minus

I can't see exactly what you need. There are not enough examples and your description of the task is not sufficient.

The number is formatted with a sign and 2 decimal places. If the last digit is a 0, it is removed with preg_replace().

$data = ['-12.20000','+18.20000', 234.0, 2.1234];

foreach($data as $value){
$formatVal = sprintf("%+0.2f",$value);
$formatVal = preg_replace('~(\.\d)0$~','$1',$formatVal);

echo $value.' -> '.$formatVal."<br>\n";
}

Output:

-12.20000 -> -12.2
+18.20000 -> +18.2
234 -> +234.0
2.1234 -> +2.12

If the result is only ever required with one decimal place, you can use

$formatVal = sprintf("%+0.1f",$value);

without the preg_replace.

php display positive number with '+' sign

You can use a helper function as following:

function getPositiveOrNegative($number){
return ($number >= 0) ? '+' : '';
}

$number = 10;
echo getPositiveOrNegative($number).$number.'<br/>';

$number = -20;
echo getPositiveOrNegative($number).$number.'<br/>';

$number = 0;
echo getPositiveOrNegative($number).$number.'<br/>';

Output:

+10

-20

+0

PHP: sprintf - zero without plus sign

There is no direct way to achieve this, as %d will consider only positive and negative sign for whatever comes to it.

But yes, there is alternative way to achieve this as follows,

echo ($voteCount === 0 ? 0 : sprintf("%+d",$voteCount));

This should solve your problem.

EDIT(As suggested by AliveToDie) :

You can do the same by using gmp_sign.

Here is reference example of it.

// positive
echo gmp_sign("500") . "\n";

// negative
echo gmp_sign("-500") . "\n";

// zero
echo gmp_sign("0") . "\n";

For the same, you need to enable extension in php.ini

extension=php_gmp.so

Print a sequence of numbers in PHP with the correct (+/-) sign

The easiest way would be to correct the operator appearance in the final string:

$s = '5 + -10 + 12 + -18 + 9 + - 7'; // result of interpolation or concatenation
$s = str_replace('+ -', '- ', $s);
// => "5 - 10 + 12 - 18 + 9 - 7"

If this is possible for you, this is as fast as it gets -- no looping (in php), no treating each number one at a time with a conditional. If you loop anyway, I'd recommend @Phils suggestion with array_reduce -- functional style php -- adapted to your needs.

Anytime scraped (php) output is positive or negative number, color accordingly

Try this simplest one. Hope this is understandable and is what you are looking for. You need to use intval,

You should define a function like this and invoke it when you need.

Try this code snippet here

<html>
<head>
<style>
.value-positive {
color: #0cdb02;
}

.value-negative {
color: #fe0000;
}
</style>
</head>
<body>
<?php

$output='-40.3%';

addClass($output);

function addClass($output)
{
$classname = intval($output) < 0 ? 'negative' : 'positive';
print "<span class='value-$classname'>$output</span>";
}

?>
</body>

</html>

Formatting a number with leading zeros in PHP

Use sprintf :

sprintf('%08d', 1234567);

Alternatively you can also use str_pad:

str_pad($value, 8, '0', STR_PAD_LEFT);

PHP prepend leading zero before single digit number, on-the-fly

You can use sprintf: http://php.net/manual/en/function.sprintf.php

<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>

It will only add the zero if it's less than the required number of characters.

Edit: As pointed out by @FelipeAls:

When working with numbers, you should use %d (rather than %s), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.

For example:

sprintf("%04s", 10); returns 0010

sprintf("%04s", -10); returns 0-10

Where as:

sprintf("%04d", 10); returns 0010

sprintf("%04d", -10); returns -010

How do I put extra number at the end of username if duplicate

First, lock the table so no other table will write to it at the same time. then do something like this:

$name = 'foo';
$first_name = $name;
$i = 0;
do {
//Check in the database here
$exists = exists_in_database($name);
if($exists) {
$i++;
$name = $first_name . $i;
}
}while($exists);
//save $name

Another method is to select all names in the table starting with "foo" and ending in a number and then finding the largest number. This can be done in SQL.

The first method is better for use cases with only a small risk of collision, since the pattern matching may be slow, but if you have a lot of collisions the latter may be better.



Related Topics



Leave a reply



Submit