收藏
回答

怎样将数字转换为字符串?

框架类型 问题类型 操作系统 工具版本
小程序 需求 Windows 1.02.180250

var newPwd = parseInt(phone) + parseInt(uPwd) + parseInt(uMin)

这里计算出newPwd是数字类型,我想取这个数字最后两位数出来,有什么好办法 吗?

我原是想将这个newPwd转换为字符串,再取后面两位的,但又不会转为字符串类型。请指教,谢谢!

var lPwd = newPwd.tostring(); 这样写出错了,正确写法是如何的?




回答关注问题邀请回答
收藏

4 个回答

  • 再见,电脑崽
    再见,电脑崽
    2018-11-22

    Math.round((newPwd/100 - Math.floor(newPwd / 100))*100)


    2018-11-22
    有用 1
    回复 4
    • 2018-11-22

      谢谢!这方法更简单。

      2018-11-22
      回复
    • 2018-11-22回复

      Math.round((newPwd/100 - Math.floor(newPwd / 100))*100)

      可以解释一下这代码的意思吗?  看不明白。例如,若要取1位,是如何修改呢?谢谢!

      2018-11-22
      回复
    • 再见,电脑崽
      再见,电脑崽
      2018-11-22回复

      取一位  Math.round((newPwd/10 - Math.floor(newPwd / 10))*10)

      取三位  Math.round((newPwd/1000 - Math.floor(newPwd / 1000))*1000)

      2018-11-22
      1
      回复
    • 2018-11-22回复再见,电脑崽

      谢谢!

      2018-11-22
      回复
  • 2018-11-22

    解决了,“拾忆”说得对。没留意,tostring 的 s 小字了,应大写:toString 。谢谢各位!

    2018-11-22
    有用
    回复
  • 拾忆
    拾忆
    2018-11-22

    我猜可能是

    var lPwd = newPwd.toString();

    或者

    var lPwd = String(newPwd);

    2018-11-22
    有用
    回复
  • ==
    ==
    2018-11-22

    出错?出啥错?要保留两位,你用toFixed()啊

    2018-11-22
    有用
    回复 3
    • 2018-11-22

      newPwd.tostring is not a function;at pages/pwdP/pwdP page getPwd function

      TypeError: newPwd.tostring is not a function

          at s.getPwd (http://127.0.0.1:53652/appservice/pages/pwdP/pwdP.js:35:23)

          at s.<anonymous> (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:604843)

          at s.onLoad (http://127.0.0.1:53652/appservice/pages/pwdP/pwdP.js:20:10)

          at s.<anonymous> (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:604105)

          at Ct (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:620997)

          at Tt (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:622155)

          at Function.<anonymous> (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:624000)

          at dt.<anonymous> (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:592901)

          at dt.emit (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:208945)

          at Object.emit (http://127.0.0.1:53652/appservice/__dev__/WAService.js:1:218332)

      console.error @ VM2749:1

      errorReport @ WAService.js:1

      thirdErrorReport @ WAService.js:1

      (anonymous) @ WAService.js:1

      (anonymous) @ WAService.js:1

      onLoad @ pwdP.js? [sm]:18

      (anonymous) @ WAService.js:1

      Ct @ WAService.js:1

      Tt @ WAService.js:1

      (anonymous) @ WAService.js:1

      (anonymous) @ WAService.js:1

      dt.emit @ WAService.js:1

      emit @ WAService.js:1

      (anonymous) @ WAService.js:1

      (anonymous) @ WAService.js:1

      e @ appservice?t=1542861544376:1141

      n.registerCallback.t @ appservice?t=1542861544376:1141

      l.forEach.t @ appservice?t=1542861544376:1141

      (anonymous) @ appservice?t=1542861544376:1141

      s.onmessage @ appservice?t=1542861544376:1141


      2018-11-22
      回复
    • ==
      ==
      2018-11-22回复

      要保留2位用toFixed()

      2018-11-22
      回复
    • ==
      ==
      2018-11-22回复

      注意大小写

      2018-11-22
      回复
登录 后发表内容