如何确保 parseInt() 函数不会 return 意外结果

How to make sure parseInt() function does not return unexpected results

以下是将毫秒转换为人类可读时间的方法。但是我收到一条评论说它可能 return 对于某些基数会产生意想不到的结果。

function msToTime(duration, sign) {
  var milliseconds = parseInt((duration % 1000) / 100),
    seconds = parseInt((duration / 1000) % 60),
    minutes = parseInt((duration / (1000 * 60)) % 60),
    hours = parseInt((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? " " + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return minutes + sign + seconds;
}

$("div").text(msToTime(1203400, ":"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div></div>

这是我收到的评论:"The parseInt() function can return undesired and/or unexpected results if the radix is not supplied.Please make sure a radix is used on all parseInt() instances."

我尝试添加天、周、月、年等等,但我不确定这是否正确。它也适用于音乐的持续时间。所以我认为不会有超过几个小时的案例。

有什么想法可以确保该方法的结果始终正确吗?

Radix 是 parseInt() 的第二个参数,它是一个数字(从 2 到 36),代表要使用的数字系统。您应该使用 10,因为它代表十进制数字系统。

var milliseconds = (parseInt(duration) % 1000) / 100, 10)

如果没有传递基数,将使用以下内容:

If the string begins with "0x", the radix is 16 (hexadecimal).

If the string begins with "0", the radix is 8 (octal). This feature is deprecated

If the string begins with any other value, the radix is 10 (decimal)

如果你不想总是指定它,创建一个这样的函数:

function parseInt10(val){
    return parseInt(val,10);
}

parseInt() 将名为 radix 的第二个参数作为其第二个参数

An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

由于你处理的是十进制数系统,所以将基数作为10来解决这样的问题

parseInt((duration % 1000) / 100, 10)

其他答案为您提供了一些关于 parseInt() 的建议,这在技术上是正确的,但与您的实际代码无关,因为您甚至不需要在函数中使用 parseInt()

因为看起来你已经将一个数字(不是字符串)传递给你的函数,你甚至不需要任何 parseInt() 操作并且可以像这样简化它:

function msToTime(duration, sign) {
  var milliseconds = (duration % 1000) / 100,
    seconds = (duration / 1000) % 60,
    minutes = (duration / (1000 * 60)) % 60,
    hours = (duration / (1000 * 60 * 60)) % 24;

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? " " + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return minutes + sign + seconds;
}

如果您希望您的函数在持续时间内接受传入的数字或字符串,您可以这样支持:

function msToTime(t, sign) {
  var duration = +t;
  var milliseconds = (duration % 1000) / 100,
    seconds = (duration / 1000) % 60,
    minutes = (duration / (1000 * 60)) % 60,
    hours = (duration / (1000 * 60 * 60)) % 24;

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? " " + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return minutes + sign + seconds;
}

而且,如果您确实使用了 parseInt(),您会将持续时间转换为数字:

var d = parseInt(duration, 10);

然后,在您的计算中使用该值。正如您的代码现在所具有的那样,您正在根据数学计算的结果调用 parseInt(),这意味着您正在调用 parseInt 一个不执行任何操作的数字。