【问题标题】:Changing a function to also work for negative values?将函数更改为也适用于负值?
【发布时间】:2020-01-29 09:57:27
【问题描述】:

我的应用程序中有一个函数,它接受一个值并以一种特殊的方式将其输出。它可以计算一个值是相同的值,但拆分为“小时:分钟”。

我需要该函数也适用于负值 - 类似于函数内部的 if 检查以检查输入的时间是否为负,然后更改计算/输出。

这是一个非常简单的函数:

   const calcSingle = time => {        
      // insert a if check somewhere here to check for time and if its negative
      let hour = Math.floor(time / 60);
      let minutes = time % 60;

      hour = hour < 10 ? "0" + hour : hour;
      minutes = minutes < 10 ? "0" + minutes : minutes;

      return hour + ":" + minutes;
  };

例如,如果我调用它:

calcSingle(200)我返回“03:20”,这是正确的值。

但是,如果我尝试calcSingle(-200),我会得到:“0-4:0-20”,这显然是错误的,因为它应该是相同的值但带有一个减号,所以这个 =>“-03:20”。

const calcSingle = time => {
  // insert a if check somewhere here to check for time and if its negative
  let hour = Math.floor(time / 60);
  let minutes = time % 60;

  hour = hour < 10 ? "0" + hour : hour;
  minutes = minutes < 10 ? "0" + minutes : minutes;

  return hour + ":" + minutes;
};
console.log(
  calcSingle(200)
)


console.log(
  calcSingle(-200)
)

编辑:感谢你们所有人的所有回复,我将使用 math.abs 它解决了我的问题!伟大的帮助家伙 - 有一个美好的一天!

【问题讨论】:

  • 出于好奇,为什么它需要使用负数?除此之外,为什么不在第一行的time 变量周围使用Math.abs
  • 您将时间作为负数传递,因此当将 0 连接到小时或分钟时,您对小时和分钟的计算为负数,然后 "0" + -hour 将设置为 0-hour:0-minute
  • @Jhecht 因为我需要显示我们的工人工作了多少小时,他们一个月的工作时间可能为负数:)
  • 你是如何工作负时间的???
  • @Jhecht 如果你必须在一个月内获得 150 小时,可以说,但你只工作 145 小时,而不是下个月开始的 -05:00

标签: javascript reactjs react-native math time


【解决方案1】:

您可以在开头添加检查并调用相同的功能更改标志。

  if (time < 0) {
    return `-${calcSingle(Math.abs(time))}`;
  }

这样

const calcSingle = time => {
  if (time < 0) {
    return `-${calcSingle(Math.abs(time))}`;
  }

  // insert a if check somewhere here to check for time and if its negative
  let hour = Math.floor(time / 60);
  let minutes = time % 60;

  hour = hour < 10 ? "0" + hour : hour;
  minutes = minutes < 10 ? "0" + minutes : minutes;

  return hour + ":" + minutes;
};

console.log(calcSingle(200));
console.log(calcSingle(-200));

【讨论】:

  • 其实挺好的
  • 我想我会选择这个,因为它只多了 2 行代码,并且做了我想要的,你能解释一下“-”是如何自动设置的吗?
  • @yesIamFaded,实际上是在使用模板字符串的结果之前添加“-”。 (返回-${calcSingle(Math.abs(time))})适用时。希望这会有所帮助。
【解决方案2】:

我认为Math.abs() 可以解决您的问题:

const calcSingle = time => {     
  let isNegative = time < 0;
  let _time = Math.abs(time);
  // insert a if check somewhere here to check for time and if its negative
  let hour = Math.floor(_time / 60);
  let minutes = _time % 60;

  hour = hour < 10 ? "0" + hour : hour;
  minutes = minutes < 10 ? "0" + minutes : minutes;

  return (isNegative ? '-':'') + hour + ":" + minutes;
};


console.log(
  calcSingle(200)
)


console.log(
  calcSingle(-200)
)

【讨论】:

    【解决方案3】:

    最简单的版本

    const pad = num => ("0" + num).slice(-2);
    const calcSingle = time => {
      let _time = Math.abs(time);
      let hour = pad(Math.floor(_time / 60));
      let minutes = pad(_time % 60);
      return (time < 0 ? '-' : '') + hour + ":" + minutes;
    };
    
    console.log(
      calcSingle(200)
    )
    console.log(
      calcSingle(-200)
    )

    【讨论】:

      【解决方案4】:

      Math.abs 可以使用。 Math.abs()

      const calcSingle = time => {
        // insert a if check somewhere here to check for time and if its negative
        let _time = Math.abs(time);
        let hour = Math.floor(_time / 60);
        let minutes = _time % 60;
      
        hour = hour < 10 ? "0" + hour : hour;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        return (time < 0 ? '-' : '') + hour + ":" + minutes;
      };
      console.log(calcSingle(200), calcSingle(-200));

      【讨论】:

        【解决方案5】:

        您可以使用Math.abs() 来获得time 的绝对值。然后,您可以在您的返回语句中检查您的初始时间是否小于 0(因此为负数)并使用它来返回正时间或负时间。

        您可以使用以下代码:

        const calcSingle = time => {
          const absTime = Math.abs(time);
          let hour = Math.floor(absTime / 60);
          let minutes = absTime % 60;
        
          hour = hour < 10 ? "0" + hour : hour;
          minutes = minutes < 10 ? "0" + minutes : minutes;
        
          return `${time < 0 ? "-" : ""}${hour}:${minutes}`;
        };
        

        【讨论】:

        • 我想我们至少在 10 分钟前都得出了相同的结论 ;)
        【解决方案6】:

        使用布尔值存储值并在结果末尾附加减号

        calcSingle = time => {
          var bIsNegative = false;
          if (time < 0) {
            bIsNegative = true;
          }
          time = Math.abs(time);
          // insert a if check somewhere here to check for time and if its negative
          let hour = Math.floor(time / 60);
          let minutes = time % 60;
        
          hour = hour < 10 ? "0" + hour : hour;
          minutes = minutes < 10 ? "0" + minutes : minutes;
        
          return bIsNegative ? "-" + (hour + ":" + minutes) : hour + ":" + minutes;
        };
        console.log(
          calcSingle(200)
        )
        console.log(
          calcSingle(-200)
        )

        【讨论】:

        • 我会亲自重写它,以便 bIsNegative 最初是一个空字符串,否则 - 可以重写最后一行`return bIsNegative + hour + ":" + minutes `
        • abs应该是Math.abs
        猜你喜欢
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-26
        相关资源
        最近更新 更多