58

How would I display positive number such as 3 as +3 and negative numbers such -5 as -5? So, as follows:

1, 2, 3 goes into +1, +2, +3

but if those are

-1, -2, -3 then goes into -1, -2, -3

Blender
  • 289,723
  • 53
  • 439
  • 496
Soso
  • 581
  • 1
  • 4
  • 3
  • 3
    How do you want to display `0`? Most seem to assume that it should be displayed as just `0` rather than `+0`. – Guffa Dec 03 '10 at 16:05

10 Answers10

58

You can use a simple expression like this:

(n<0?"":"+") + n

The conditional expression results in a plus sign if the number is positive, and an empty string if the number is negative.

You haven't specified how to handle zero, so I assumed that it would be displayed as +0. If you want to display it as just 0, use the <= operator instead:

(n<=0?"":"+") + n
Community
  • 1
  • 1
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
23
// Forces signing on a number, returned as a string
function getNumber(theNumber)
{
    if(theNumber > 0){
        return "+" + theNumber;
    }else{
        return theNumber.toString();
    }
}

This will do it for you.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
20

A modern solution would be to use Intl.NumberFormat

const myNumber = 5;

new Intl.NumberFormat("en-US", {
    signDisplay: "exceptZero"
}).format(myNumber);

depending on what myNumber is it will display positive or negative sign, except when it's a 0.

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
  • 1
    Would be ideal but has only 87% browser support. Safari & Chrome on iOS didn't support this until 2021-04-26. – alextes Aug 05 '21 at 15:13
6
printableNumber = function(n) { return (n > 0) ? "+" + n : n; };
01001111
  • 838
  • 5
  • 5
4

write a js function to do it for you?

something like

var presentInteger = function(toPresent) {
    if (toPresent > 0) return "+" + toPresent;
    else return "" + toPresent;
}

you could also use the conditional operator:

var stringed = (toPresent > 0) ? "+" + toPresent : "" + toPresent;

Thanx to the comments for pointing out that "-" + toPresent would put a double -- on the string....

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
3
function format(n) {
    return (n>0?'+':'') + n;
}
Samuel
  • 2,106
  • 1
  • 17
  • 27
3
['','+'][+(num > 0)] + num

or

['','+'][Number(num > 0)] + num

It is a shorter form than the ternary operator, based on casting boolean to the number 0 or 1 and using it as an index of an array with prefixes, for a number greater than 0 the prefix '+' is used

0

something along the lines of:

if (num > 0)
{
   numa = "+" + num;
}
else
{
   numa = num.toString();
}

and then print the string numa.

Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
0

The solution with the ternary operator seems just fine, but for the fun of it, here is another one:

('+'+x).replace("+-", "-");
-1

Modern syntax solution.

It also includes a space between sign and number:

function getNumberWithSign(input) {
  if (input === 0) {
    return "0"
  }

  const sign = input < 0 ? '-' : '+';

  return `${sign} ${Math.abs(input)}`;
}
neiker
  • 8,887
  • 4
  • 29
  • 32
  • While this code may provide a solution to OP's problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution. – E. Zeytinci Feb 20 '20 at 20:34