JavaScript: Display positive numbers with the plus sign(JavaScript:用加号显示正数)
问题描述
如何将正数(如 3)显示为 +3,将负数(如 -5)显示为 -5?所以,如下:
How would I display positive number such as 3 as +3 and negative numbers such -5 as -5? So, as follows:
1、2、3进入+1、+2、+3
1, 2, 3 goes into +1, +2, +3
但如果是的话
-1, -2, -3 然后进入 -1, -2, -3
-1, -2, -3 then goes into -1, -2, -3
推荐答案
你可以使用这样的简单表达式:
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.
你还没有指定如何处理零,所以我假设它会显示为 +0
.如果您只想将其显示为 0
,请改用 <=
运算符:
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
这篇关于JavaScript:用加号显示正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript:用加号显示正数


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01