本文介绍了增加一个数字而不会消除前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取一个值,并将其重用为该数字具有前导零的数字,并且仅限于4位数字,表示格式为0001001001001000例如,当我向该数字加1时,我将从零开始计数,例如以加法的方式去掉前导零

I am getting a value from database and I am reusing it as a number this number has leading zeroes and it is limited to only 4 digits meaning to say the format is 0001 or 0010 or 0100 or 1000 I am starting the count from zero when I add 1 to this number the leading zeroes are gone the way I add is for example

var databasevalue = 0000;
var incrementvalue = parseInt(databasevalue) + parseInt(1);

推荐答案

// I suppose databasevalue is a string
var databasevalue = "0125";

// coerce the previous variable as a number and add 1
var incrementvalue = (+databasevalue) + 1;

// insert leading zeroes with a negative slice
incrementvalue = ("0000" + incrementvalue).slice(-4); // -> result: "0126"

这篇关于增加一个数字而不会消除前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 23:04