最近单位需要,开始学习微信小程序,写个Demo,里面有一个计时器,当我拿到当前时间的时候 因为是second 要转换成xx:xx:xx,需要运算,
于是我就用保留小数的sec.toFixed(0)
由于是toFixed()函数是四舍五入的,所以计算出来的时间并不正确。
找了一圈之后发现一个函数 parseInt(sec); 这个函数只保留整数部分,不会四舍五入。先记录一下
var app = getApp();
Page({
data: {
second:0,
a:12.623,
b:123.56,
c:16.6,
},
onReady: function () {
wx.setNavigationBarTitle({
title: '我的'
})
},
keepInt:function(){
a1 = this.data.a;
var b1 = this.data.b;
var c1 = this.data.c;
console.log("toFixed(0) :a1 = " + a1)
console.log("toFixed(2) :b1 = " + b1)
console.log("parseInt(c1) :c1 = " + c1)
console.log("==========结果")
a1 = a1.toFixed(0);
b1 = b1.toFixed(2);
c1 = parseInt(c1);
console.log("a1 = "+a1)
console.log("b1 = " + b1)
console.log("c1 = " + c1)
},
onLoad:function(){
var that =this;
// that.countdown();
that.keepInt();
}
});
结果: