js division calculation upload progress shows multiple decimal places

The way to calculate the file upload progress:
file upload progress = (backend feedback successfully received size / total file size) 100 + "%"

There is nothing wrong with the above rules, but! ! ! When the word progress is displayed along with the progress bar, disharmonious numbers and extra-long decimals appear: for example, 7.0000000000004, even if toFixed is used, this will still happen. What's even more outrageous is that, for example, the current progress calculation result is 7.0000000000004, and the next second progress calculation result is 7.00. The co-authoring progress has been rolled back? ┑( ̄Д  ̄)┍

Finally process the number displayed on the progress bar with a string

let mustUp = 0;//防止数字反弹,显示数字回退的不良体验,同时也是进度条滚动进度的参考
let recentUp = (res.lodaded / res.total).toFixed(2);//toFixed大部分时间能起作用
mustUp = recentUp > mustUp ? recentUp : mustUp;
let str = (mustUp * 100).toString().slice(0,4) == '100.' ? '100' : (mustUp * 100).toString().slice(0,4);
let uploadRation =  str  + '%';

Guess you like

Origin blog.csdn.net/weixin_43939111/article/details/130951794