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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…