php 如何在PHP中用加号前缀一个正数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2682397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:20:40  来源:igfitidea点击:

How to prefix a positive number with plus sign in PHP

phpregexstringnumber-formatting

提问by user318466

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/printso the quicker the better.

这个函数会被多次调用,echo/print所以越快越好。

Update:

更新:

Thank you all for the answers. I must tell the sprintfbased solution is really fast.

谢谢大家的回答。我必须告诉sprintf基于的解决方案真的很快。

回答by codaddict

You can use regex as:

您可以将正则表达式用作:

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

But I would suggest notusing regexfor such a trivial thing. Its better to make use of sprintfhere as:

但我建议不要使用regex这种微不足道的东西。最好在这里使用sprintf作为:

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

From PHP Manual for sprintf:

来自sprintf 的 PHP 手册

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 attachedas well, and was added in PHP 4.3.0.

强制在数字上使用符号(- 或 +)的可选符号说明符。默认情况下,如果数字是负数,则仅在数字上使用 - 符号。此说明符强制正数也附加 + 号,并在 PHP 4.3.0 中添加。

回答by Dal Hundal

function formatNum($num) {
   return ($num>0)?'+'.$num:$num;
}

回答by pinkgothic

function formatNum($num) {
  $num = (int) $num; // or (float) if you'd rather
  return (($num >= 0) ? '+' : '') . $num; // implicit cast back to string
}

回答by rancho

The simple solution is to make use of format specifier in printf() function.

简单的解决方案是在 printf() 函数中使用格式说明符。

For example,

例如,

$num1=2;
$num2=-2;
printf("%+d",$num1);
echo '<br>';
printf("%+d",$num2);

gives the output

给出输出

+2
-2

In your case

在你的情况下

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

回答by nikc.org

The sprintfsolution provided by @unicornaddict is very nice and probably the most elegant way to go. Just thought I'd provide an alternative anyway. Not sure how they measure up in speed.

sprintf@unicornaddict 提供的解决方案非常好,可能是最优雅的方式。只是想无论如何我都会提供替代方案。不确定他们如何衡量速度。

// Non float safe version
function formatNum($num) {
    return (abs($num) == $num ? '+' : '') . intval($num);
}

// Float safe version
function formatNum($num) {
    return 
        (abs($num) == $num ? '+' : '') 
        . (intval($num) == $num ? intval($num) : floatval($num));
}

// Float safe version, alternative
function formatNum($num) {
    return 
        (abs($num) == $num ? '+' : '') 
        // Add '1' to $num to implicitly cast it to a number
        . (is_float($num + 1) ? floatval($num) : intval($num));
}