简体   繁体   English

如何在时间表中保持领先零

[英]How to keep leading zero in a timetable

I've got a selection of times, but I want to keep the leading zero:我有选择的时间,但我想保持前导零:

var fastTrainReading = [0943, 0957, 1006, 1013 , 1027, 1036, 1043, 1057, 1106, 1113, 1127, 1136, 1213, 1227, 1236, 1243, 1257, 1306, 1313, 1327, 1336, 1343, 1357, 1406, 1413, 1427, 1436, 1443, 1457, 1506, 1513, 1527, 1537, 1543, 1559, 1606, 1613, 1627, 1636, 1643, 1657, 1704, 1718, 1728, 1735, 1749, 1758, 1816, 1830, 1847, 1859, 1906, 1911, 1930, 1936, 1941, 1959, 2006, 2017, 2027];

This is the math performed:这是执行的数学运算:

var currentTime = hour*100 + mins;
if ((day == 0) || (day == 6)) {
    document.write ("There are no buses today");
}  else {

var displayCount = 0;
        var TrainStr1 = "";
        for (var i=0, len=fastTrainReading.length; i<len; ++i) {
            if ((fastTrainReading[i] > currentTime) && (displayCount < 2)) {
                displayCount = displayCount+1;
                TrainStr1=TrainStr1 + fastTrainReading[i] + "<br/>";
            }
        }
    }
document.write (TrainStr1)

I had a pretty good search through, if I missed something feel free to abuse me (but point me in the right direction).我进行了很好的搜索,如果我错过了什么可以随意辱骂我(但指出我正确的方向)。

Simplest solution is to store your time data as strings eg var fastTrainReading = ['0943', ... .最简单的解决方案是将您的时间数据存储为字符串,例如var fastTrainReading = ['0943', ... JavaScript will cast to integer for you in your calculation routines. JavaScript 将在您的计算例程中为您转换为 integer。

For a comprehensive string formatting solution that adheres to conventional principles, try sprintf() for javascript: http://www.diveintojavascript.com/projects/javascript-sprintf要获得符合常规原则的全面字符串格式化解决方案,请尝试 sprintf() for javascript: http://www.diveintojavascript.com/projects/javascript-sprintf

You can try to use .toString()您可以尝试使用.toString()
like:喜欢:
TrainStr1=TrainStr1 + fastTrainReading[i].toString() + "<br/>"; TrainStr1=TrainStr1 + fastTrainReading[i].toString() + "<br/>";
alt to save your times as strings. alt 将您的时间保存为字符串。

By default you won't get the leading zeroes.默认情况下,您不会得到前导零。

As you know the length of TrainStr1 is 4, you can use the following function to get zeroes.如您所知 TrainStr1 的长度为 4,您可以使用以下 function 来获取零。

function formatted(time) {
    var s = "0000" + time;
    return s.substr(s.length-4); }

You can call the function 'formatted' before using document.write您可以在使用 document.write 之前调用 function 'formatted'

You need to zero pad your numbers.你需要对你的数字进行零填充。

Number.prototype.zf = function _zeroFormat(digits)
{
    var n = this.toString(), pLen = digits - n.length;
    for ( var i = 0; i < pLen; i++)
    {
        n = '0' + n;
    }
    return n;
}

if ((fastTrainReading[i] > currentTime.zf(4)) && (displayCount < 2)) {
   displayCount = displayCount+1;
   TrainStr1=TrainStr1 + fastTrainReading[i] + "<br/>";
}

Once you've normalized all of your numbers to be 0-padded to 4 digits, string comparison is possible.一旦您将所有数字标准化为 0 填充为 4 位数字,就可以进行字符串比较。 Otherwise, you'll have issues.否则,你会有问题。 As things stand, it looks like your code was trying to compare a string (like an element from fastTrainReading) and a number (currentTime).就目前情况而言,您的代码似乎试图比较一个字符串(如来自 fastTrainReading 的元素)和一个数字(currentTime)。

Just declare your array as strings:只需将您的数组声明为字符串:

var fastTrainReading = ['0943', '0957', '1006', '1013'];

And don't worry fastTrainReading[i] > currentTime will still work.不用担心fastTrainReading[i] > currentTime仍然有效。 '100' > 99 == true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM