小程序
小游戏
企业微信
微信支付
扫描小程序码分享
var newPwd = parseInt(phone) + parseInt(uPwd) + parseInt(uMin)
这里计算出newPwd是数字类型,我想取这个数字最后两位数出来,有什么好办法 吗?
我原是想将这个newPwd转换为字符串,再取后面两位的,但又不会转为字符串类型。请指教,谢谢!
var lPwd = newPwd.tostring(); 这样写出错了,正确写法是如何的?
4 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
Math.round((newPwd/100 - Math.floor(newPwd / 100))*100)
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
谢谢!这方法更简单。
可以解释一下这代码的意思吗? 看不明白。例如,若要取1位,是如何修改呢?谢谢!
取一位 Math.round((newPwd/10 - Math.floor(newPwd / 10))*10)
取三位 Math.round((newPwd/1000 - Math.floor(newPwd / 1000))*1000)
谢谢!
解决了,“拾忆”说得对。没留意,tostring 的 s 小字了,应大写:toString 。谢谢各位!
我猜可能是
var lPwd = newPwd.toString();
或者
var lPwd = String(newPwd);
出错?出啥错?要保留两位,你用toFixed()啊
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
onLoad @ pwdP.js? [sm]:18
Ct @ WAService.js:1
Tt @ WAService.js:1
dt.emit @ WAService.js:1
emit @ 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
要保留2位用toFixed()
注意大小写
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
Math.round((newPwd/100 - Math.floor(newPwd / 100))*100)
谢谢!这方法更简单。
Math.round((newPwd/100 - Math.floor(newPwd / 100))*100)
可以解释一下这代码的意思吗? 看不明白。例如,若要取1位,是如何修改呢?谢谢!
取一位 Math.round((newPwd/10 - Math.floor(newPwd / 10))*10)
取三位 Math.round((newPwd/1000 - Math.floor(newPwd / 1000))*1000)
谢谢!
解决了,“拾忆”说得对。没留意,tostring 的 s 小字了,应大写:toString 。谢谢各位!
我猜可能是
var lPwd = newPwd.toString();
或者
var lPwd = String(newPwd);
出错?出啥错?要保留两位,你用toFixed()啊
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
要保留2位用toFixed()
注意大小写