微信小程序-求整数


最近单位需要,开始学习微信小程序,写个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();
  }
});

结果:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值